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