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