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