I'm trying to write a function that will take 2 integers as variables startNum
and endNum
and return an array containing all the integers in between the 2 variables.
So for example if we input (3,6)
we should get as a return an array [3,4,5,6]
. For this problem we assume that startNum
will always be smaller than EndNum
. Also if startNum
and endNum
are the same, the function should still work.
I've written the following code but i get an error in the console.
function rangeOfNumbers(startNum, endNum) {
return startNum === 0 && endNum === 0 ?[]: [startNum,rangeOfNumbers(startNum 1),...endNum];
}
I get this error message: RangeError: Maximum call stack size exceeded.
Can anyone tell me why it's not working?
CodePudding user response:
A simple enough change should make the recursive approach work, we keep on calling recursively until the startNum is greater than the endNum.
function rangeOfNumbers(startNum, endNum) {
return startNum > endNum ? [] : [startNum, ...rangeOfNumbers(startNum 1, endNum)];
}
console.log(JSON.stringify(rangeOfNumbers(1, 5)));
console.log(JSON.stringify(rangeOfNumbers(2, 2)));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Also, here's a simple way to generate a number range using Array.from()
.
We generate the resulting array, first by specifying the length, then using the key passed to the populating callback to create the actual contents.
function rangeOfNumbers(startNum, endNum) {
return Array.from( { length: (endNum - startNum 1) }, (v,k) => startNum k);
}
console.log(JSON.stringify(rangeOfNumbers(1, 5)));
.as-console-wrapper { max-height: 100% !important; top: 0; }
CodePudding user response:
function rangeOfNumbers(startNum, endNum)
{
//Add this condition for avoid stack issue
if(startNum >=endNum)
return [];
if (endNum - startNum === 2)
return [startNum 1];
let list = rangeOfNumbers(startNum, endNum - 1);
list.push(endNum - 1);
return list;
};
CodePudding user response:
Small change in your function to check if startNum
is greater than endNum
, return empty array and increment startNum
at every recursion call.
function rangeOfNumbers(startNum, endNum) {
if (startNum > endNum) {
return [];
}
return [startNum, ...rangeOfNumbers(startNum 1, endNum)];
}
console.log(rangeOfNumbers(3, 6));
CodePudding user response:
function rangeOfNumbers(startNum, endNum) {
if (startNum == endNum) {
return [startNum] // Base case
}
return [startNum, ...rangeOfNumbers(startNum 1, endNum)]
}
console.log(JSON.stringify(rangeOfNumbers(3, 6)));
A helpful tip is to figure out what the base case and the subtask are.
In this case, the base case is the situation where startNum
== endNum
. A one-element list should be returned.
As for the subtask, you may decompose the task like this:
rangeOfNumbers(3, 6)
= [3, ...rangeOfNumbers(4, 6)]
= [3, ...[4, ...rangeOfNumbers(5, 6)]]
//... and so on
CodePudding user response:
You don't need recursion to achieve that. You can do it like this:
function rangeOfNumbers(start, end) {
var result = [];
for (let i = start; i <= end; i ) {
result.push(i);
}
return result;
}
console.log(rangeOfNumbers(4,8)); // Outputs: [4, 5, 6, 7, 8]