I've encountered an issue with two functions I have. Both of them generate a range from a starting integer to an ending integer, inclusive. Here's how they're defined:
function createRange(start, end) {
var result = [];
for (var i = start; i <= end; i++) {
result.push(i);
}
return result;
}
console.log(createRange(1,5));
//Expected output [1, 2, 3, 4, 5]
function functionalCreateRange(start, end) {
function helper(result, strt, nd) {
if (strt > nd) return [];
if (strt === nd) {
return result;
} else {
return helper(result.concat([strt]), strt + 1, nd);
}
}
return helper([], start, end);
}
console.log(functionalCreateRange(1,5));
//Expected output [1, 2, 3, 4, 5]
The first function works as expected without any issues. However, the second one throws an error saying "TypeError: result.push is not a function." This has left me puzzled because I'm passing an array as the first argument to the helper function and using the correct parameter name within the helper function. Since it is indeed an array, I am unsure why this error is occurring.