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