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