Tell us what’s happening:
Why do we need concat method at all? Can someone help me solving this issue without concat?
**Your code so far**
var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
// Only change code below this line
var newArr= globalArray.slice(0,5)
return newArr;
newArr.sort(function(a,b){
a-b;
})
// Only change code above this line
}
nonMutatingSort(globalArray);
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36
Challenge: Return a Sorted Array Without Changing the Original Array
You don’ t need to use the .concat method, but it is one way to do it.
Calling .slice() on an array without any parameters makes a duplicate of the array. Sorting this new array does not change the original array.
Here is part of the challenge description regarding mutation:
One way to avoid this is to first concatenate an empty array to the one being sorted
It only suggest one of the ways you can sort an array, without mutating it, but retruning a new array. Spread, slice etc, are all other valid methods and each one can have its particular use and be more appropriate in a certain situation, so its a good thing to be able to apply each one of them and have them in your arsenal