codewars

Codewars - Solution to "Transportation on vacation" in JavaScript

Below is my solution to the Codewars "Transportation on vacation" challenge.

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 function that gives out the total amount for different days (d).

First, we check to see if we get the day discount ($20) by seeing if the number of days d is between 3 and 7. Next, we check to see if we get the week discount ($50) by seeing if the number of days are greater than or equal to 7. Finally, we return (days rented) * $40 - (day discount) - (week discount).

function rentalCarCost(d) {  
  let day = d >= 3 && d < 7 ? 20 : 0, // day discount
      week = d >= 7 ? 50 : 0; // week discount

  return (d * 40) - day - week;
}

more JavaScript posts