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