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