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