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