0

I have a function, rangeInclusive, which accepts two args:

function rangeInclusive(start, end) {
   ...
}

In order to calculate the start and end values I have another function called getValidYearRange:

getValidYearRange() {
  let validRange = [];
  if (myCondition === 'foo') {
    validRange = [this.calculateValidYear1(), this.calculateValidYear2()];
  } else {
    validRange = [1900, 2017];
   }

   return validRange;
}

As you can see I have a condition set that determines whether the range is my hardcoded values, or calls another two functions to get more info.

However I get the following problem, rangeInclusive seems to get the arguments as an array and undefined, e.g:

start = [1900, 2017], end = undefined rather than: start = 1900, end = 2017

I cannot change rangeInclusive at all as it's used throughout the project.

How can I return two values for start and end without calling functions twice?

user1486133
  • 1,309
  • 3
  • 19
  • 37
  • You can't return more than one value. You need to change how you are calling `rangeInclusive`, eg `rangeInclusive(returnVal)` do `rangeInclusive(...returnVal)` – Patrick Evans Jul 11 '17 at 12:57
  • Can't you just do: `rangeInclusive(validRange[0], validRange[1]) { /* do something */ }` or `rangeInclusive(...validRange) { /* do something */ }` – StudioTime Jul 11 '17 at 13:01
  • I can't see one line where rangeInclusive gets called, that's where you can change something. Please show more of your code. – Rob Jul 11 '17 at 13:02

2 Answers2

1

If you are using es6 syntax you could to this by

const [start, end] = getValidYearRange()

Now you have the two variables start and end with which you can call rangeInclusive

0

Try like this

[start,end] = getValidYearRange();

and refer this link : https://stackoverflow.com/a/2917483/4798278