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