Functional Programming - Return a Sorted Array Without Changing the Original Array

Tell us what’s happening:
I dont know why this code is not passing the test.

Your code so far

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

function nonMutatingSort(arr) {
  // Only change code below this line
 return arr.slice().sort()

  // Only change code above this line
}

console.log(nonMutatingSort(globalArray));
console.log(globalArray)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.34

Challenge: Functional Programming - Return a Sorted Array Without Changing the Original Array

Link to the challenge:

Have you looked at the results from cases that are not passing test?

console.log(nonMutatingSort([1, 30, 4, 21, 100000]));
console.log(nonMutatingSort([140000, 104, 99]));

You need to look at how sort actually sorts: it’s not based on numbers, I think this is covered in an earlier challenge.

Note that the slice isn’t necessary any more in up to date browsers: toSorted is the equivalent to sort that doesn’t mutate

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.