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