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