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