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