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