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