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