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