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