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