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