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