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