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