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