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