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