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