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