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