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