Returning a value from a function


#1

Hello All,

I have the following function:

  getMonth(month) {
    this.calendarProvider.getMonths().then(data => {
      let months = data[0].months_of_year;
      for (let i = 0; i < months.length; i++) {
        if (i === month) {
          console.log(months[i]);
        }
      }
    });
  }

This function is called like so:

let now = new Date();
let month = this.getMonth(now.getMonth());

It is using a json file that is laid out like so:

[{
  "months_of_year": [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
  ]
}]

Can some please tell me how to edit my function so that ii will return a string.


#2
getMonth(month) {
    this.calendarProvider.getMonths().then(data => {
      let months = data[0].months_of_year;
      return month < months.length ? months[month] : '';
    });
  }
let now = new Date();
this.getMonth(now.getMonth()).then(month => {
  console.log('yay, a month!', month);
});