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

Tell us what’s happening:
const sortArray = convertArrayToNumbers.sort((a,b) => a - b);
is not sorting properly

I dont know if I have the wrong format for sorting numbers

Your code so far

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

function nonMutatingSort(arr) {
  // Only change code below this line
    const copyArray = arr.join("").split("");
    const convertArrayToNumbers = [];
    for(let i = 0; i < copyArray.length; i++){
      const stringToNumberConverter = Number(copyArray[i]);
      convertArrayToNumbers.push(stringToNumberConverter)
    }
    const sortArray = convertArrayToNumbers.sort((a,b) => a - b);
    return sortArray;
  // Only change code above this line
}

nonMutatingSort(globalArray);
console.log(nonMutatingSort([1, 30, 4, 21, 100000]))

Your browser information:

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

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

Link to the challenge:

to copy an array, i would use arr.slice()

1 Like

Even after you fix the problem with creating copyArray, you are still not going to pass the fourth test. I think your code might be confusing the tests. Also, I think your code is overly complex. You can solve this in one line without creating any temporary variables. I would go for that option as it seems to make the tests happy.

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