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