How to add a conditional in the code below in case there are no dates in this range, it assigns a fixed value for costRate?
function getPrices(allSeasons, checkin, checkout) {
const day = 1000*60*60*24;
return allSeasons.reduce( (totalPrice, season) => {
let daysInSeason = Math.round(
(Math.min(+season.endDate+day, checkout)
- Math.max(season.startDate, checkin)) / day);
return totalPrice + (daysInSeason > 0 && daysInSeason * season.costRate);
}, 0);
}
const allSeasons = [
{startDate: new Date(2017, 1-1, 1), endDate: new Date(2017, 4-1,30), costRate: 300},
{startDate: new Date(2017, 5-1, 1), endDate: new Date(2017, 9-1,30), costRate: 400},
{startDate: new Date(2017,10-1, 1), endDate: new Date(2017,12-1,31), costRate: 500}
],
checkin = new Date(2017, 9-1,30),
checkout = new Date(checkin.getTime() + (48 * 60 * 60 * 1000)),
totalPrice = getPrices(allSeasons, checkin, checkout);
console.log(totalPrice);