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