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