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