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