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