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