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