Can't create range of numbers via recursion. last lesson of basic JS

Tell us what’s happening:
my code won’t work, stranger thing is that it gives 2 as final output, no matter what you change. I don’t understand, WHY.

Your code so far


function rangeOfNumbers(startNum, endNum) {
if (startNum == endNum){
   return [startNum];
}else if (startNum < endNum ){
   //console.log(startNum)
   var shit = [startNum]
   var nextNum = rangeOfNumbers((startNum + 1), endNum)[0];
   console.log(nextNum);
   return shit.push(nextNum);
 }
 
}
// function rangeOfNumbers(startNum, endNum) {
//   if (endNum - startNum === 0) {
//     return [startNum];
//   } else {
//     var numbers = rangeOfNumbers(startNum, endNum - 1);
//     numbers.push(endNum);
//     return numbers;
//   }

// }
var array = rangeOfNumbers(4, 7);
///array.push(1,1);
console.log(array);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36.

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-range-of-numbers

one of your issues is that push return a number

the other one:
when you have rangeOfNumbers(6, 9)
you have that shit equals [6]
rangeOfNumbers(7,9) should equal [7,8,9] and newNum equals 7
shit.push(newNum) makes shit have value of [6,7] but the push method returns the new length of the array, so you are returning 2

then I also imagine that you could have an error because also rangeOfNumbers(7,9) returned a number, and you are trying to read the element at index 0 of a number, which is impossible and throw an error

1 Like

thank you very much. I didn’t knew that push() statement also returned the new lenght of the array. you helped me in starting debuging process in the right direction. my fundamental problem was that I couldn’t realize actual backwards process, correct order in which things are going on in recursion.

it is that one

if you start with rangeOfNumbers(6,9) inside it there will be a call to rangeOfNumbers(7,9) or to rangeOfNumbers(6,8)

let’s say it is this second one
so rangeOfNumbers(6,9) should return [6,7,8,9]

inside it you call rangeOfNumbers(6,8) that returns [6,7,8], do something with this value to get the desired result, and return

you also need to establish for what numbers or relationship between the numbers the function will just return something without calling again the function

hi again,
i did try to do the recursion, i dont know whats wrong with my code. But let me explain, how i did it…
At first im initiating a count with the startNumber, and then in the IF condition i am checking if my count has increased more than the end number, if so im returning an empty array. (honestly i dont know why im returning an empty array).
ELSE, im increasing the count variable, and then pushing the count to the array, after which im calling the function itself. Obviously the code is not working the way i wanted it work. Please explain to me where im wrong.
thank you

function rangeOfNumbers(startNum, endNum) {
  var count = startNum;
  if(count >= endNum){
    return [];
  } else {

    count++;
    var arr = [];
    arr.push(count);
    return rangeOfNumbers(startNum + count);
  }
};

The starting number will always be less than or equal to the ending number.
So far with each recursion number we have being doing this. Remeber your first lesson on recursion? Since we are using zero indexing
0 1 2 3 4 5
-1 0 1 2 3 4
As we learned before we used a -1
Where is your starting number?
I can see a function name named like starting number but what is it?

The function rangeOfNumbers have two parameters, so you need to give it two arguments, or it doesn’t work - here you have called it only with one argument