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