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