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