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