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