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