0

I want to get the variables values based on the parameter passed to the function. Is there any way to do this:

 getDaysInCurrentMonth(type = 'all') {
      const date = new Date(),
            month = date.getMonth(),
            year = date.getFullYear(),
            days = new Date(year, month, 0).getDate();
      if (type === 'all') return { date, month, year, days };
      return [type];
  }

For example, if I pass days in the getDaysInCurrentMonth('days'), it should only return days. Is there any way to do this?

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35

4 Answers4

1

If you want to return a single parameter, you could create an object and then access it by key. For example

getDaysInCurrentMonth(type = 'all') {
  const date = new Date(),
        month = date.getMonth(),
        year = date.getFullYear(),
        days = new Date(year, month, 0).getDate();
  const result = { date, month, year, days };
  if (type === 'all') return result;
  else return result[type];
}

You would have to be sure to only call the method with the specified types or add error handling though.

Smorgaz
  • 26
  • 4
1

You can do something like this.

getDaysInCurrentMonth(type = 'all') {
  const date = new Date(),
        month = date.getMonth(),
        year = date.getFullYear(),
        days = new Date(year, month, 0).getDate();
  const result = { date, month, year, days };
  return (type === 'all') ? result : result[type]
}
Awais
  • 331
  • 3
  • 11
0

Yes, there is.

Your end function should look something like this:

const getDaysInCurrentMonth = (type = 'all') => {
     const date = new Date(),
            month = date.getMonth(),
            year = date.getFullYear(),
            days = new Date(year, month, 0).getDate();
    switch(type){
        case "all": return { date, month, year, days };
        case "month": return { month };
        case "date": return { date };
        case "year": return { year };
        default: return `Unknown type ${type}`;
    }
}
Shannarra
  • 521
  • 4
  • 17
0

You can try using a dictionary and save your values in Key-Value pairs where key is the parameter you accept as an argument to the function.

function getDaysInCurrentMonth(type = 'all') {
            const obj = {};
      const date = new Date;
      const month = date.getMonth();
      const year = date.getFullYear();
      
        obj["date"] = date;
      obj["month"] = month;
      obj["year"] = year;
      obj["days"] = new Date(year, month, 0).getDate();
      
      return obj[type] || date.toLocaleString();

  }
 
 console.log(getDaysInCurrentMonth("year"));
 console.log(getDaysInCurrentMonth("month"));
 console.log(getDaysInCurrentMonth("days"));
ahsan
  • 1,409
  • 8
  • 11
  • It does not look very DRY; if you have `const month = date.getMonth();` then you can do `obj["month"] = month;`, there is no point in doing `obj["month"] = date.getMonth();` and why should you generate a `new Date` to give a value to `obj["days"]`, when yu already have `date`? – secan Aug 20 '21 at 11:16
  • @secan I have simply used the provided code by the OP. I wanted to provide a solution with minimum modifications so its easier to understand. – ahsan Aug 20 '21 at 11:24