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