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