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