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