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