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