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