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

Tell us what’s happening:
Describe your issue in detail here
return arr.slice().sort();
This method results [ 1, 100000, 21, 30, 4 ] as its sorted array
why does this method displays wrong sorted array ??sort is not proper if one of the element is too large??
Your code so far

const globalArray = ([1, 30, 4, 21, 100000]);

function nonMutatingSort(arr) {
  // Only change code below this line
  // return [].concat(arr).sort(function(a,b){
  //   return a-b
  // }
  // );
  return arr.slice().sort();
  // Only change code above this line
}

console.log(nonMutatingSort(globalArray));

Your browser information:

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

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

Link to the challenge:

i know not to change global array but i tried to solve this challenge by using slice and sort method .it worked properly with deafult global array as input but i was not not able to sort this array properly .

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