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