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