Problem
- Every day you rent the car costs $40.
- If you rent the car for 7 or more days, you get $50 off your total.
- 7일 이상 렌트하면, 총 금액에서 $50이 할인된다.
- Alternatively, if you rent the car for 3 or more days, you get $20 off your total.
- 3일 이상 렌트하면, 총 금액에서 $20이 할인된다.
- Write a code that gives out the total amount for different
days
.
Solution 01
function rentalCarCost(days) {
if (days >= 7) return days * 40 - 50;
if (days >= 3 && days < 7) return days * 40 - 20;
if (days <= 3) return days * 40;
}
rentalCarCost(1); // 40
rentalCarCost(2); // 80
rentalCarCost(3); // 100 (120 - 20)
rentalCarCost(4); // 140
rentalCarCost(5); // 180
rentalCarCost(6); // 220
rentalCarCost(7); // 230 (280 - 50)
rentalCarCost(8); // 270
rentalCarCost(9); // 310
rentalCarCost(10); // 350
Solution 02
function rentalCarCost(days) {
return days * 40 - (days >= 7 ? 50 : days >= 3 ? 20 : 0);
}
rentalCarCost(1); // 40
rentalCarCost(2); // 80
rentalCarCost(3); // 100 (120 - 20)
rentalCarCost(4); // 140
rentalCarCost(5); // 180
rentalCarCost(6); // 220
rentalCarCost(7); // 230 (280 - 50)
rentalCarCost(8); // 270
rentalCarCost(9); // 310
rentalCarCost(10); // 350