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