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