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