Return a Sorted Array Without Changing the Original Array slice instead of concat

Return a Sorted Array Without Changing the Original Array slice instead of concat
0.0 0

#1

Tell us what’s happening:

I understand, that the challenge was created to use concat method, but would such solution with slice(0,) be correct outside of FCC?

Your code so far


var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
  // Add your code below this line
  
  const asc = (a,b)=>{a-b};
  return arr.slice(0,).sort(asc);
  
  // Add your code above this line
}
nonMutatingSort(globalArray);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/return-a-sorted-array-without-changing-the-original-array


#2

Hi Bogdan,

First I think your code about the arrow function is faulty, the sort compare delegate should return an int value, since your function doesn’t. so the fix could be like

const asc = (a,b)=>{return a-b};

Looks ok, since FCC needs you to work wit hFCC, so test fails, even if your return is the one it expected.

I’m not a JS expert, and don’t know diffs between concat and slice, and copy, I think they are kind of alias.

Keep going on great work, happy programming.