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