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