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