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