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