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