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