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
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.
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