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