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