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