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