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