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