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