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