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