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