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