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