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