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