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