-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnonMutatingSort.js
More file actions
20 lines (15 loc) · 814 Bytes
/
nonMutatingSort.js
File metadata and controls
20 lines (15 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// *** Functional Programming: Return a Sorted Array Without Changing the Original Array ***
// 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 concat returns a new array), then run the sort method.
// 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.
var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
// Add your code below this line
return arr.concat().sort();
// Add your code above this line
}
nonMutatingSort(globalArray);