I am working on FCC challenges, learning JS. Here is the link of challenge that cofuses me.
I kinda solved it, but one code that should work, while other does not, and I would assume they are essentially the same things.
This is a code that works:
var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
// Add your code below this line
return [].concat(arr).sort(function(a, b) {
return a - b;
});
// Add your code above this line
}
nonMutatingSort(globalArray);
And this code does not work
var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
// Add your code below this line
let newArr = [];
newArr.concat(arr);
return newArr.sort(function(a,b){return a-b;});
// Add your code above this line
}
nonMutatingSort(globalArray);
My question is essentially why? Both codes concatenate old array to new one, and both functions should return sorted array.
However in first function concatenation fails... it returns only empty arr. Why? I am so confused. It works outside of the function, however not in function.