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