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