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