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