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