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