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