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