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