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