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