JSFiddle - React, Tailwind, and code Playground

by joplomacedo

JavaScript

function monthSelectOptions( foundingYear, foundingMonth, currentYear, currentMonth ) {
    const years = ( () => {
      const years = [];
      let year = foundingYear;

      while ( year <= currentYear ) {
        years.push(year);
      }

      return years;
    })();

    const monthsByYear = years.map( year => {
      const months = [];

      if ( year === foundingYear) {
        month = foundingMonth;

      } else {
        month = 0;
      }

      while ( month <= currentMonth ) {
        months.push(month);
        month++;
      }

      return months.map( month => month + ' ' + year);
    })
    
    return monthsByYear;
  }
  
console.log(monthSelectOptions(2020,0, 2020, 1));