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