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