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