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