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