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