So, I successfully did the bonus part of the More Functions studio where I was tasked with rewriting my number array sorter with recursion. However, with recursion I was unable to initialize the empty array that the function returns inside the function. I had to initialize it as a global variable.
Just curious, is it possible at all to initialize the empty array inside the function while using recursion? Here’s what I have with the empty array initialized outside the function.
let sortedArray = [];
function numberSort(arr){
if (arr.length === 0) {
return sortedArray;
} else {
sortedArray.push(findLowestValue(arr));
arr.splice(arr.indexOf(findLowestValue(arr)),1);
return numberSort(arr);
}
}