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