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