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