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

Tell us what’s happening:

Describe your issue in detail here.
I am not mutating global variable but still why this code is failing?

Your code so far

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

function nonMutatingSort(arr) {
  // Only change code below this line
let newArr=[...arr];   
return newArr.sort((a,b)=>{
  return a-b  
})

  // 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/119.0.0.0 Safari/537.36

Challenge Information:

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

It seems you change the globalArray? You just need to set it back to the original values and you should be fine.

If you want to test some values, I recommend to comment the original values and create a new one so that you don’t have to reset the lesson:

// const globalArray = [5, 6, 3, 2, 9];     <-- original values
const globalArray = [1, 30, 4, 21, 100000]; <-- new values for testing

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

  let newArr=[...arr];   
  return newArr.sort((a,b)=>{
    return a-b  
2 Likes

woo hoo thanks u saved me I was gona pull my hairs!

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