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