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