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 times does this code print “Hi”?
for (let i = 1; i < 6; i++) {
console.log("Hi");
} -
2.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 3; i++) {
total = total + i;
}
console.log(total); -
3.🔁 How many times does this code print “Hello”?
for (let i = 1; i <= 9; i++) {
console.log("Hello");
} -
4.🔁 What does this code print?
let score = 23;
if (score < 44) {
console.log("You win");
} else {
console.log("Play again");
} (a) You win (b) Play again - 5.🔁 Which loop repeats code a set number of times? (a) let (b) for (c) else (d) if
-
6.🔁 How many numbers does this code print?
for (let i = 0; i < 12; i = i + 3) {
console.log(i);
} -
7.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 7; i++) {
total = total + 4;
}
console.log(total); -
8.🔁 What does this code print?
let score = 12;
if (score > 18) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win - 9.🔁 What does i++ do? (a) adds 1 to i (b) doubles i (c) takes 1 from i (d) prints i
-
10.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 7; i++) {
total = total + 5;
}
console.log(total); -
11.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 8; i++) {
total = total + i;
}
console.log(total); -
12.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 9; i++) {
total = total + i;
}
console.log(total); -
13.🔁 What does this code print?
let temp = 41;
if (temp < 34) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Hot (b) Cool -
14.🔁 How many times does this code print “Welcome”?
for (let i = 2; i < 7; i++) {
console.log("Welcome");
} -
15.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 8; i++) {
total = total + 3;
}
console.log(total); -
16.🔁 What does this code print?
let age = 48;
if (age < 36) {
console.log("teen");
} else {
console.log("child");
} (a) child (b) teen -
17.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 6; i++) {
total = total + 5;
}
console.log(total); - 18.🔁 When does the else part run? (a) never (b) when if is true (c) always (d) when if is false
-
19.🔁 How many times does this code print “Hello”?
for (let i = 2; i <= 8; i++) {
console.log("Hello");
} -
20.🔁 How many numbers does this code print?
for (let i = 0; i < 15; i = i + 4) {
console.log(i);
} - 21.🔁 Which word runs code only when something is true? (a) let (b) log (c) if (d) for
-
22.🔁 What is the LAST number this code prints?
for (let i = 0; i < 16; i = i + 2) {
console.log(i);
} -
23.🔁 How many numbers does this code print?
for (let i = 0; i < 8; i = i + 2) {
console.log(i);
} -
24.🔁 What does this code print?
let age = 26;
if (age >= 8) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
25.🔁 How many times does this code print “Hello”?
for (let i = 0; i < 4; i++) {
console.log("Hello");
} -
26.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 4; i++) {
total = total + 3;
}
console.log(total); -
27.🔁 What does this code print?
let age = 41;
if (age >= 38) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
28.🔁 How many numbers does this code print?
for (let i = 0; i < 14; i = i + 2) {
console.log(i);
} -
29.🔁 How many times does this code print “Hi”?
for (let i = 3; i < 7; i++) {
console.log("Hi");
} -
30.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 5; i++) {
total = total + i;
}
console.log(total); -
31.🔁 How many times does this code print “Hello”?
for (let i = 0; i <= 3; i++) {
console.log("Hello");
} -
32.🔁 How many times does this code print “Welcome”?
for (let i = 1; i < 8; i++) {
console.log("Welcome");
} -
33.🔁 How many numbers does this code print?
for (let i = 0; i < 6; i = i + 4) {
console.log(i);
} -
34.🔁 What does this code print?
let age = 50;
if (age >= 24) {
console.log("teen");
} else {
console.log("child");
} (a) child (b) teen -
35.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 10; i++) {
total = total + 4;
}
console.log(total); -
36.🔁 How many times does this code print “Well done”?
for (let i = 0; i < 4; i++) {
console.log("Well done");
} -
37.🔁 What does this code print?
let age = 2;
if (age >= 12) {
console.log("teen");
} else {
console.log("child");
} (a) child (b) teen -
38.🔁 How many times does this code print “Well done”?
for (let i = 2; i <= 10; i++) {
console.log("Well done");
} -
39.🔁 What does this code print?
let temp = 36;
if (temp < 18) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Hot (b) Cool -
40.🔁 How many times does this code print “Well done”?
for (let i = 1; i < 9; i++) {
console.log("Well done");
}
Answer key
- 5
- 6
- 9
- You win
- for
- 4
- 28
- Play again
- adds 1 to i
- 35
- 36
- 45
- Cool
- 5
- 24
- child
- 30
- when if is false
- 7
- 4
- if
- 14
- 4
- teen
- 4
- 12
- teen
- 7
- 4
- 15
- 4
- 7
- 2
- teen
- 40
- 4
- child
- 9
- Cool
- 8
Free html and css lessons, practice and worksheets at talentjr.in/html-css-for-kids