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