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