JavaScript Worksheet: If and For Loops
Code tip (Check, then count): if (condition) { … } else { … }: only one of the two parts runs. for (let i = 0; i < 5; i++) runs for i = 0, 1, 2, 3, 4 — 5 times. With i <= n it includes n; with i = i + 2 it goes up in twos.
NameDateTime takenScore ___ / 40
-
1.🔁 How many numbers does this code print?
for (let i = 0; i < 7; i = i + 4) {
console.log(i);
} -
2.🔁 What does this code print?
let age = 33;
if (age >= 34) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
3.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 8; i++) {
total = total + i;
}
console.log(total); -
4.🔁 How many times does this code print “Well done”?
for (let i = 0; i <= 5; i++) {
console.log("Well done");
} -
5.🔁 What does this code print?
let age = 32;
if (age >= 5) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child - 6.🔁 What does i++ do? (a) doubles i (b) takes 1 from i (c) prints i (d) adds 1 to i
-
7.🔁 How many numbers does this code print?
for (let i = 0; i < 15; i = i + 2) {
console.log(i);
} - 8.🔁 Which loop repeats code a set number of times? (a) else (b) for (c) let (d) if
- 9.🔁 When does the else part run? (a) always (b) when if is false (c) never (d) when if is true
-
10.🔁 What does this code print?
let temp = 45;
if (temp < 33) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Hot (b) Cool -
11.🔁 What does this code print?
let age = 13;
if (age < 9) {
console.log("teen");
} else {
console.log("child");
} (a) child (b) teen - 12.🔁 Which word runs code only when something is true? (a) let (b) for (c) if (d) log
-
13.🔁 What does this code print?
let score = 5;
if (score >= 9) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win -
14.🔁 What does this code print?
let score = 36;
if (score < 15) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win -
15.🔁 What does this code print?
let score = 12;
if (score >= 7) {
console.log("You win");
} else {
console.log("Play again");
} (a) You win (b) Play again -
16.🔁 What does this code print?
let score = 17;
if (score > 13) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win -
17.🔁 What is the LAST number this code prints?
for (let i = 0; i < 20; i = i + 4) {
console.log(i);
} -
18.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 9; i++) {
total = total + 4;
}
console.log(total); -
19.🔁 What is the LAST number this code prints?
for (let i = 0; i < 18; i = i + 2) {
console.log(i);
} -
20.🔁 What does this code print?
let temp = 32;
if (temp >= 44) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Cool (b) Hot -
21.🔁 How many times does this code print “Well done”?
for (let i = 3; i < 6; i++) {
console.log("Well done");
} -
22.🔁 What does this code print?
let score = 21;
if (score >= 31) {
console.log("You win");
} else {
console.log("Play again");
} (a) You win (b) Play again -
23.🔁 What does this code print?
let marks = 49;
if (marks > 27) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Try again (b) Pass -
24.🔁 How many numbers does this code print?
for (let i = 0; i < 18; i = i + 2) {
console.log(i);
} -
25.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 8; i++) {
total = total + 7;
}
console.log(total); -
26.🔁 What does this code print?
let age = 49;
if (age < 42) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
27.🔁 How many times does this code print “Hello”?
for (let i = 0; i <= 9; i++) {
console.log("Hello");
} -
28.🔁 How many numbers does this code print?
for (let i = 0; i < 20; i = i + 4) {
console.log(i);
} -
29.🔁 How many numbers does this code print?
for (let i = 0; i < 20; i = i + 3) {
console.log(i);
} -
30.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 6; i++) {
total = total + 5;
}
console.log(total); -
31.🔁 How many times does this code print “Hello”?
for (let i = 3; i < 11; i++) {
console.log("Hello");
} -
32.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 7; i++) {
total = total + 2;
}
console.log(total); -
33.🔁 What does this code print?
let temp = 27;
if (temp >= 24) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Hot (b) Cool -
34.🔁 How many numbers does this code print?
for (let i = 0; i < 5; i = i + 2) {
console.log(i);
} -
35.🔁 How many times does this code print “Hello”?
for (let i = 2; i < 6; i++) {
console.log("Hello");
} -
36.🔁 How many times does this code print “Welcome”?
for (let i = 0; i < 5; i++) {
console.log("Welcome");
} -
37.🔁 What is the LAST number this code prints?
for (let i = 0; i < 8; i = i + 3) {
console.log(i);
} -
38.🔁 What is the LAST number this code prints?
for (let i = 0; i < 16; i = i + 4) {
console.log(i);
} -
39.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 4; i++) {
total = total + i;
}
console.log(total); -
40.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 9; i++) {
total = total + 3;
}
console.log(total);
Answer key
- 2
- child
- 36
- 6
- teen
- adds 1 to i
- 8
- for
- when if is false
- Cool
- child
- if
- Play again
- Play again
- You win
- You win
- 16
- 36
- 16
- Cool
- 3
- Play again
- Pass
- 9
- 56
- child
- 10
- 5
- 7
- 30
- 8
- 14
- Hot
- 3
- 4
- 5
- 6
- 12
- 10
- 27
Free html and css lessons, practice and worksheets at talentjr.in/html-css-for-kids