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