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