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