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