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