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