Skip to main content

--description--

A side effect of the sort method is that it changes the order of the elements in the original array. In other words, it mutates the array in place. One way to avoid this is to first concatenate an empty array to the one being sorted (remember that slice and concat return a new array), then run the sort method.

--instructions--

Use the sort method in the nonMutatingSort function to sort the elements of an array in ascending order. The function should return a new array, and not mutate the globalArray variable.

--hints--

Your code should use the sort method.

assert(nonMutatingSort.toString().match(/\.sort/g));

The globalArray variable should not change.

assert(JSON.stringify(globalArray) === JSON.stringify([5, 6, 3, 2, 9]));

nonMutatingSort(globalArray) should return [2, 3, 5, 6, 9].

assert(
JSON.stringify(nonMutatingSort(globalArray)) ===
JSON.stringify([2, 3, 5, 6, 9])
);

nonMutatingSort(globalArray) should not be hard coded.

assert(!nonMutatingSort.toString().match(/\[.*?[23569].*?\]/gs));

The function should return a new array, not the array passed to it.

assert(nonMutatingSort(globalArray) !== globalArray);

nonMutatingSort([1, 30, 4, 21, 100000]) should return [1, 4, 21, 30, 100000].

assert(JSON.stringify(nonMutatingSort([1, 30, 4, 21, 100000])) ===
JSON.stringify([1, 4, 21, 30, 100000]))

nonMutatingSort([140000, 104, 99]) should return [99, 104, 140000].

assert(JSON.stringify(nonMutatingSort([140000, 104, 99])) ===
JSON.stringify([99, 104, 140000]))

--seed--

--seed-contents--

const globalArray = [5, 6, 3, 2, 9];

function nonMutatingSort(arr) {
// Only change code below this line


// Only change code above this line
}

nonMutatingSort(globalArray);

--solutions--

const globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
return [].concat(arr).sort((a,b) => a-b);
}