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