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