Conditional Statements - Exercise 1
Question 1: Choose a Rain Plan Completed
Return "umbrella" if it is raining, otherwise "no umbrella". The argument is a boolean.
Expected output: "umbrella" (print this with console.log)
function chooseRainPlan(isRaining) {
// Write your solution here
}
console.log(chooseRainPlan(true));function chooseRainPlan(isRaining) {
if (isRaining) return "umbrella";
return "no umbrella";
}
console.log(chooseRainPlan(true));Question 2: Check a Library Loan Limit Completed
Return true if the number of borrowed books is below the limit.
Expected output: true (print this with console.log)
function canBorrowAnotherBook(borrowed, limit) {
// Write your solution here
}
console.log(canBorrowAnotherBook(3, 5));function canBorrowAnotherBook(borrowed, limit) {
return borrowed < limit;
}
console.log(canBorrowAnotherBook(3, 5));Question 3: Choose a Jacket Completed
Return "jacket" when the temperature is below 15 and "no jacket" otherwise.
Expected output: "jacket" (print this with console.log)
function chooseTemperatureClothing(temperature) {
// Write your solution here
}
console.log(chooseTemperatureClothing(12));function chooseTemperatureClothing(temperature) {
if (temperature < 15) return "jacket";
return "no jacket";
}
console.log(chooseTemperatureClothing(12));Question 4: Check a Booking Window Completed
Return true if daysUntilDeparture is between 1 and 30 inclusive.
Expected output: true (print this with console.log)
function isBookingWindowOpen(daysUntilDeparture) {
// Write your solution here
}
console.log(isBookingWindowOpen(10));function isBookingWindowOpen(daysUntilDeparture) {
return daysUntilDeparture >= 1 && daysUntilDeparture <= 30;
}
console.log(isBookingWindowOpen(10));Question 5: Choose a Page Size Completed
Return the supplied positive page size, or 10 when it is zero or negative.
Expected output: 10 (print this with console.log)
function choosePositivePageSize(size) {
// Write your solution here
}
console.log(choosePositivePageSize(-1));function choosePositivePageSize(size) {
if (size > 0) return size;
return 10;
}
console.log(choosePositivePageSize(-1));Question 6: Clamp a Brightness Value Completed
Return the input clamped to the inclusive range 0 to 100.
Expected output: 100 (print this with console.log)
function clampBrightness(brightness) {
// Write your solution here
}
console.log(clampBrightness(120));function clampBrightness(brightness) {
if (brightness < 0) return 0;
if (brightness > 100) return 100;
return brightness;
}
console.log(clampBrightness(120));Question 7: Check a File Extension Completed
Return true if the case-sensitive filename ends with ".js".
Expected output: true (print this with console.log)
function hasJavaScriptExtension(filename) {
// Write your solution here
}
console.log(hasJavaScriptExtension("app.js"));function hasJavaScriptExtension(filename) {
return filename.endsWith(".js");
}
console.log(hasJavaScriptExtension("app.js"));Question 8: Choose a Queue Direction Completed
Return "wait" if peopleAhead is greater than zero and "your turn" otherwise.
Expected output: "wait" (print this with console.log)
function chooseQueueMessage(peopleAhead) {
// Write your solution here
}
console.log(chooseQueueMessage(2));function chooseQueueMessage(peopleAhead) {
return peopleAhead > 0 ? "wait" : "your turn";
}
console.log(chooseQueueMessage(2));Question 9: Calculate a Member Price Completed
Return the price minus the supplied discount for members; otherwise return the full price. isMember is a boolean.
Expected output: 25 (print this with console.log)
function priceForMember(price, discount, isMember) {
// Write your solution here
}
console.log(priceForMember(30, 5, true));function priceForMember(price, discount, isMember) {
if (isMember) return price - discount;
return price;
}
console.log(priceForMember(30, 5, true));Question 10: Check a Nonempty Playlist Completed
Return true when the supplied array contains at least one track.
Expected output: true (print this with console.log)
function playlistHasTracks(tracks) {
// Write your solution here
}
console.log(playlistHasTracks(["song"]));function playlistHasTracks(tracks) {
return tracks.length > 0;
}
console.log(playlistHasTracks(["song"]));