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