Hello, is there a bug in this challenge - section JavaScript Algorithms & Data Structures subsection Functional Programming: Return a Sorted Array Without Changing the Original Array. ???
I’ve messed with this fairly simple challenge for awhile, trying to figure out why the challenge kept saying I couldn’t hardcode my solution. The only way I was able to pass this challenge was by using the solution given in the “Get Help” page.
My solution worked and was basically the same just a bit more code. I would love it if someone could take a look at this, and tell me if my solution really was / is hardcoded and what the difference might be in what the final solution was, compared to the solution I came up with.
Thanks for anyone that takes a look into this! I would really love some clarification!
This is the solution I came up with -
var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
// Only change code below this line
var arr3 = [].concat(arr);
arr3.sort(
(a, b)=> {
if (a === b) {
return 0;
} else if (a < b) {
return -1;
} else if (a > b) {
return 1;
}
}
);
return arr3;
// Only change code above this line
}
nonMutatingSort(globalArray);
It passed all the tests, except for the one saying I couldn’t hardcode.
The solution provided by FCC follows -
var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
// Add your code below this line
return [].concat(arr).sort(function(a, b) {
return a - b;
});
// Add your code above this line
}
nonMutatingSort(globalArray);```