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