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