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