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