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