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