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