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