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