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