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