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