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