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