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