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