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