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