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