Why I can't do this?

Thanks for helping me out :heart:
Why I can do this:

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

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

nonMutatingSort(globalArray);

But I can’t do this? What’s the difference?

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

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

nonMutatingSort(globalArray);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Return a Sorted Array Without Changing the Original Array

Link to the challenge:

Log this and see what it’s returning.


You’re doing this correctly but you’re actually not assigning it to the empty array. Thus, the empty array you declared is still empty.

1 Like

I got what you mean!! so I still need to assign it to newGlobalArray, right?
I texted it, It worked!
Thank you very much!

1 Like