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