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