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