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