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