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