JS Exam 2: Conditions and Loops (10 mixed exercises)
JS Exam 2: Control Flow and Loops
Test your ability to make decisions and repeat actions using conditions and loops.
Exercise 1
If/Else Statements
Write code to check age:
- Define a variable `age` with a value.
- If age is greater than or equal to 18, print "Adult".
- Otherwise, print "Minor".
Solution
let age = 20;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
Exercise 2
Else If Chains
Evaluate a student's score:
- Define a variable `score` (from 0 to 100).
- If the score >= 90 print "Excellent".
- If it is >= 75 print "Very good".
- If it is >= 50 print "Pass".
- Otherwise print "Fail".
Solution
let score = 85;
if (score >= 90) {
console.log("Excellent");
} else if (score >= 75) {
console.log("Very good");
} else if (score >= 50) {
console.log("Pass");
} else {
console.log("Fail");
}
Exercise 3
Switch Statement
Use `switch` to check the day:
- Define a variable `day` containing the day number (1-7).
- If it's 1 print "Sunday", 2 "Monday", and so on.
- Add a default case that prints "Invalid day".
Solution
let day = 3;
switch (day) {
case 1:
console.log("Sunday");
break;
case 2:
console.log("Monday");
break;
case 3:
console.log("Tuesday");
break;
default:
console.log("Invalid day");
}
Exercise 4
For Loop
Use a `for` loop to print numbers:
- Print the numbers from 1 to 10.
- Print only the even numbers in that range.
Solution
for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) {
console.log(i);
}
}
Exercise 5
While Loop
Use a `while` loop for a countdown:
- Start from 5.
- Print the number and decrease it by 1 each iteration.
- Stop when the number reaches 0 and print "Lift off!".
Solution
let count = 5;
while (count > 0) {
console.log(count);
count--;
}
console.log("Lift off!");
Exercise 6
Looping Arrays
You have an array `fruits = ["Apple", "Banana", "Orange"]`:
- Use a `for` loop to print each element.
- Use a `for...of` loop to do the same in a modern way.
Solution
let fruits = ["Apple", "Banana", "Orange"];
// Traditional loop
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
// for...of
for (let fruit of fruits) {
console.log(fruit);
}
Exercise 7
Loop Control (Break & Continue)
Inside a loop from 1 to 10:
- Skip printing the number 5 (continue).
- Stop the loop entirely when reaching 8 (break).
Solution
for (let i = 1; i <= 10; i++) {
if (i === 5) {
continue; // skip
}
if (i === 8) {
break; // stop
}
console.log(i);
}
Exercise 8
Ternary Operator
Rewrite the following in one line:
- If `isMember` is true, set `fee` to 10.
- Otherwise set `fee` to 20.
Solution
let isMember = true;
let fee = isMember ? 10 : 20;
console.log(fee);
Exercise 9
Do...While Loop
Use `do...while`:
- Define a variable `i = 0`.
- Print the number and increment it by 1.
- Continue while `i` is less than 3 (note it runs at least once).
Solution
let i = 0;
do {
console.log(i);
i++;
} while (i < 3);
Exercise 10
Looping Object Properties (For...in)
You have an object `person = {name: "Ali", age: 30}`:
- Use a `for...in` loop to iterate over all properties.
- Print each key and value (for example: "name: Ali").
Solution
let person = { name: "Ali", age: 30 };
for (let key in person) {
console.log(key + ": " + person[key]);
}