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