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