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