Conditional Statements - Exercise 2
Question 1: Label a Battery Level Completed
Return "low" below 20, "medium" from 20 through 79, and "high" at 80 or above.
Expected output: "medium" (print this with console.log)
function batteryLevelLabel(level) {
// Write your solution here
}
console.log(batteryLevelLabel(50));function batteryLevelLabel(level) {
if (level < 20) return "low";
if (level < 80) return "medium";
return "high";
}
console.log(batteryLevelLabel(50));Question 2: Choose a Parcel Category Completed
Return "small" for weight up to 2, "medium" for weight up to 10, and "large" for greater weight.
Expected output: "medium" (print this with console.log)
function parcelWeightCategory(weight) {
// Write your solution here
}
console.log(parcelWeightCategory(5));function parcelWeightCategory(weight) {
if (weight <= 2) return "small";
if (weight <= 10) return "medium";
return "large";
}
console.log(parcelWeightCategory(5));Question 3: Classify a Temperature Alert Completed
Return "freezing" below 0, "hot" above 35, and "normal" otherwise.
Expected output: "hot" (print this with console.log)
function temperatureAlertLabel(temperature) {
// Write your solution here
}
console.log(temperatureAlertLabel(36));function temperatureAlertLabel(temperature) {
if (temperature < 0) return "freezing";
if (temperature > 35) return "hot";
return "normal";
}
console.log(temperatureAlertLabel(36));Question 4: Label a Delivery Status Completed
Return "arrived" for delivered, "on the way" for shipped, and "processing" for any other status.
Expected output: "on the way" (print this with console.log)
function deliveryStatusLabel(status) {
// Write your solution here
}
console.log(deliveryStatusLabel("shipped"));function deliveryStatusLabel(status) {
if (status === "delivered") return "arrived";
if (status === "shipped") return "on the way";
return "processing";
}
console.log(deliveryStatusLabel("shipped"));Question 5: Calculate a Parking Fee Completed
Return 0 for up to 1 hour, 5 for more than 1 and up to 3 hours, and 10 for more than 3 hours. The input is non-negative.
Expected output: 5 (print this with console.log)
function parkingFeeForHours(hours) {
// Write your solution here
}
console.log(parkingFeeForHours(2));function parkingFeeForHours(hours) {
if (hours <= 1) return 0;
if (hours <= 3) return 5;
return 10;
}
console.log(parkingFeeForHours(2));Question 6: Choose a Time Greeting Completed
Return "morning" for hours below 12, "afternoon" for hours below 18, and "evening" otherwise. The hour is an integer from 0 to 23.
Expected output: "afternoon" (print this with console.log)
function daytimeGreetingLabel(hour) {
// Write your solution here
}
console.log(daytimeGreetingLabel(13));function daytimeGreetingLabel(hour) {
if (hour < 12) return "morning";
if (hour < 18) return "afternoon";
return "evening";
}
console.log(daytimeGreetingLabel(13));Question 7: Check a Password Length Completed
Return "short" for fewer than 8 characters, "long" for more than 20, and "acceptable" otherwise.
Expected output: "short" (print this with console.log)
function passwordLengthLabel(password) {
// Write your solution here
}
console.log(passwordLengthLabel("abc"));function passwordLengthLabel(password) {
if (password.length < 8) return "short";
if (password.length > 20) return "long";
return "acceptable";
}
console.log(passwordLengthLabel("abc"));Question 8: Choose a Transit Fare Completed
Return 0 for ages under 5, 2 for ages 5 through 17, and 4 for ages 18 and over.
Expected output: 2 (print this with console.log)
function transitFareByAge(age) {
// Write your solution here
}
console.log(transitFareByAge(10));function transitFareByAge(age) {
if (age < 5) return 0;
if (age < 18) return 2;
return 4;
}
console.log(transitFareByAge(10));Question 9: Choose a Low Stock Action Completed
Return "reorder" when quantity is below the minimum, "full" when it is above the maximum, and "ok" otherwise.
Expected output: "reorder" (print this with console.log)
function stockLevelAction(quantity, min, max) {
// Write your solution here
}
console.log(stockLevelAction(2, 5, 20));function stockLevelAction(quantity, min, max) {
if (quantity < min) return "reorder";
if (quantity > max) return "full";
return "ok";
}
console.log(stockLevelAction(2, 5, 20));Question 10: Evaluate a Goal Completed
Return "exceeded" when actual is greater than target, "met" when equal, and "not met" otherwise.
Expected output: "exceeded" (print this with console.log)
function evaluateGoal(actual, target) {
// Write your solution here
}
console.log(evaluateGoal(12, 10));function evaluateGoal(actual, target) {
if (actual > target) return "exceeded";
if (actual === target) return "met";
return "not met";
}
console.log(evaluateGoal(12, 10));