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