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