/* why are we assigning the recursive call in numbers variable, can’t we just call the recursive fn and push… let numbers = recursive call*/
function rangeOfNumbers(startNum, endNum) {
if(startNum>endNum)
{
return [];
}
else
{
let numbers = rangeOfNumbers(startNum,endNum-1);
/* why are we assigning the recursive call in numbers variable, can't we just call the recursive fn and push.*/
numbers.push(endNum);
return numbers;
}
};
console.log(rangeOfNumbers(6,9))
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
I mean that if i call the recursive function(without assigning it to the numbers variable) and then push the values in the array and then if i return the array (return numbers), will it not work ?
This won’t work because numbers won’t be an array.
const arr = [10, 20, 30]
arr.push(40) //arr is mutated and it is now [10, 20, 30, 40]
const nums = [10, 20, 30].push(40) //nums is 4, the number of elements in nums