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