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