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