Transportation on vacation

Lee·2022년 9월 8일

Algorithm

목록 보기
91/92
post-thumbnail

❓ Transportation on vacation

Q. After a hard quarter in the office you decide to get some rest on a vacation. So you will book a flight for you and your girlfriend and try to leave all the mess behind you.

You will need a rental car in order for you to get around in your vacation. The manager of the car rental makes you some good offers.

Every day you rent the car costs $40. If you rent the car for 7 or more days, you get $50 off your total. Alternatively, if you rent the car for 3 or more days, you get $20 off your total.

Write a code that gives out the total amount for different days(d).

✔ Solution

//#my solution
function rentalCarCost(d) {
  // Your solution here
  let total = 40;
  if (d < 3) {
    return total * d;
  } else if (d >= 3 && d < 7) {
    return total * d - 20;
  } else if (d >= 7) {
    return total * d - 50;
  }
}

//#other solution
const rentalCarCost = d => d * 40 - ((d > 6) ? 50 : ((d > 2) ? 20 : 0));
profile
Lee

1개의 댓글

comment-user-thumbnail
2025년 4월 24일
답글 달기