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