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