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