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