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