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