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