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