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