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