DEV Community

Discussion on: Daily Challenge #291 - Extended Weekend

Collapse
 
_bkeren profile image
'' • Edited
  • To create a range as an array in JS with inclusive borders , you can use Array(end + 1).keys , then slice it from the start number.
  • For extended month, only look for months which takes 31 days.
  • In JS Date object, month index starts from 0. Jan => 0 , Feb => 1 and so on.
  • In JS Date object, week day index starts from 0 with Sunday, so Friday's index is 5.
const solve = (a,b) => {
 const FRIDAY = 5, JAN = 0, MAR = 2 , MAY = 4, JUL = 6, AUG = 7, OCT = 9, DEC = 11
 const months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
 const extendedMonths = [...Array(b+1).keys()].slice(a).map(year => ([JAN,MAR,MAY,JUL,AUG,OCT,DEC].filter(monthIndex => new Date(year,monthIndex,1).getDay() === FRIDAY)).map(monthIndex => `${months[monthIndex]} ${year}`)).flat()
 const firstExtendedMonth = extendedMonths.length > 0 ? extendedMonths[0].split(" ")[0] : ""
 const lastExtendedMonth = extendedMonths.length > 0 ? extendedMonths[extendedMonths.length - 1].split(" ")[0] : ""
 return [firstExtendedMonth,lastExtendedMonth,extendedMonths.length]
}