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