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