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