2

I want to get as a result [1, 2, 3, 4, 5] in array. What am doing wrong? Do I have to decrease endNum?

function rangeOfNumbers(startNum, endNum) {
  if (endNum - startNum >= 0){
    const array = rangeOfNumbers(startNum, endNum);
    array.push(startNum);
    return rangeOfNumbers(startNum + 1, endNum);
  } else {
    return [];
  }
};
console.log(rangeOfNumbers(1, 5));
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
sayinmehmet47
  • 490
  • 5
  • 21
  • As a comment, it's certainly not the right way to use recursion – Chayim Friedman Feb 01 '21 at 20:57
  • recursion blowed my mind certainly.if i understand it clearly,i would have deep inside:) – sayinmehmet47 Feb 01 '21 at 21:00
  • Does this answer your question? [JavaScript function similar to Python range()](https://stackoverflow.com/questions/8273047/javascript-function-similar-to-python-range) – rickdenhaan Feb 01 '21 at 21:00
  • 2
    @rickdenhaan I think that the question is why isn't this working, not how to solve the general problem – Chayim Friedman Feb 01 '21 at 21:01
  • How do you expect this to ever terminate? If `endNum - startNum` is more or equal to zero then `endNum - startNum + 1` would also be. – VLAZ Feb 01 '21 at 21:02
  • @ChayimFriedman Fair enough, I've been bashed lately for "anwering obvious dupes" so maybe I'm growing too quick to mark questions as duplicates now :-) – rickdenhaan Feb 01 '21 at 21:05
  • You get stuck in an infinite loop because the first thing your function does is to call itself with the exact same parameters – Lennholm Feb 01 '21 at 21:11
  • thanks i took my answer.Really it is my second question here.Very helpful and informative.I will use different version of solving method – sayinmehmet47 Feb 01 '21 at 21:17

5 Answers5

3
console.log([...Array(6).keys()].slice(1))

OUTPUT: [1, 2, 3, 4, 5]

"Under the hood" an array in JavaScript is actually an object with keys that are integers. The code spreads the keys of an array of length 6 to another array.

The slice method returns the second through the last element.

If you are looking for a range function, try this:

var range = (start, stop, step=1) => {
    const length = Math.ceil((stop - start) / step);
    return Array.from({length}, (_, i) => (i * step) + start);
}

range(1, 6)

OUTPUT: [1, 2, 3, 4, 5]

dmmfll
  • 2,666
  • 2
  • 35
  • 41
2

This should do the job with for loop.

function rangeOfNumbers(a, b){
 let arr = [];
 for(a; a<=b; a++){
  arr.push(a)
 }
 return arr;
}
grudinsky
  • 108
  • 6
0

Short and concise:

Read about this method in this great anwer

const rangeOfNumbers = (a,b) => [...Array(b+1).keys()].slice(a)

console.log( rangeOfNumbers(1,5) )
console.log( rangeOfNumbers(5,10) )

The above isn't ideal when the parameters are quite large numbers, the code will needlessly generate a large array only for a small slice:

rangeOfNumbers(1000000,1000005)

Let's try to make a function which is more "considerate" on resources:

const rangeOfNumbers = (a,b) => [...Array(b-a)].map((_,i) => i+a+1)

console.log( rangeOfNumbers(1000000, 1000005) )
vsync
  • 118,978
  • 58
  • 307
  • 400
  • Hi @Alex - OP did not specify he wants recursion in the title not the description of his question, therefore I am surprised you say OP wants that, and even so, if he does, then OP did not convey that in any way. – vsync Feb 01 '21 at 21:22
  • 2
    I was guessing he tried to solve it with recursion because that just happened to be what OP came up with. I try to go by the title of questions, since it should be the essence and future people will come here from Google seeking help and click **because** of the title, and they probably will gain more value from my answer than the selected one. – vsync Feb 01 '21 at 21:44
0

Getting range numbers using recursion in JavaScript

function rangeOfNumbers(startNum, endNum) {
 if (startNum - endNum === 0) {
  return [startNum];
 } else {
  const numbers = rangeOfNumbers(startNum + 1, endNum);    
  numbers.unshift(startNum);
  return numbers;
 }
};
CodeNewbie
  • 173
  • 1
  • 10
0

try this :

 const array=Array();
    function rangeOfNumbers(startNum, endNum) {
      if(endNum-startNum>=0){
        console.log(startNum)
        array.push(startNum);
       return rangeOfNumbers(startNum+1,endNum);
        
      }else{
        return array;
      }
    };
    console.log(rangeOfNumbers(1,5));
Belhadjer Samir
  • 1,461
  • 7
  • 15