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