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