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