Skip to content

Latest commit

 

History

History
35 lines (29 loc) · 1.02 KB

8 kyu - Transportation on vacation.md

File metadata and controls

35 lines (29 loc) · 1.02 KB

Task

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

def rental_car_cost(d)
  total = d * 40
  return total -= 50 if d > 6
  d > 2 ? (return total -= 20) : total
end

Explanation

  1. Total is calculated as days * 40
  2. Return total by minus 50 if the days are greater than 6
  3. If the days are only greater by 2 then return by minus 50
  4. Else return the total

Alternate solution

def rental_car_cost(d)
  return d * 40 if d < 3
  return d * 40 - 20 if d < 7
  return d * 40 - 50
end