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