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