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