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