Return a Sorted Array Without Changing the Original Array - Functional Programming - Free Code Camp

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Trying to get a job in web design and ur helping so much really appreciated

paulfanning
Автор

The answer has been changed and or updated Mr. Ian to this
const globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
return [].concat(arr).sort((a, b) => a - b);
}

zken
Автор

But this would not work if you had for example 10 in that array,
then 10 would be first number in new array because numbers are converted to strings.
You should do it with callback function. Here is mine solution:

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


function nonMutatingSort(arr) {
// Only change code below this line
let newArray = [...arr];

function compareFunction (a, b){
return a-b;
};
return

// Only change code above this line
}

nonMutatingSort(globalArray);

gorank
Автор

another option

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

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

// Only change code above this line
}

nonMutatingSort(globalArray);

zacharyfranczak
Автор

this method doesnt pass the test. Thanks

kita