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