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