nonMutatingSort(globalArray) should not be hard coded

What does that exactly mean?I tries out different ways…I copied the passed argumnet into another array and used that array…But I got the same error

nonMutatingSort(globalArray) should not be hard coded.

Thank You!

My Code


var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
// Only change code below this line
 //var arr2 =arr.concat([]);
 //console.log(arr2);
// return [...globalArray].sort(function(a,b) { return a-b;});
//var globalArray = [5, 6, 3, 2, 9];
var arr2 = [...arr];
return [].concat(arr2).sort(function(a, b) {
  return a - b;
});
 //console.log(arr2)
 //console.log(arr)
 //return arr2;
// Only change 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/84.0.4147.105 Safari/537.36.

Challenge: Return a Sorted Array Without Changing the Original Array

Link to the challenge:

“Hard coded” means relying on specific values instead of variables. In this case, the requirement exists because many people tried solving this problem by referencing globalArray instead of using arr. In your case, the fact that you are copying arr into arr2 and using arr2 in your return is falsely triggering that check. If you remove var2 =[...arr]; and use arr in your return statement, you’ll pass the tests.

Bugs like this can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it.

@ArielLeslie Thank You for your reply.
I had already tried the same direclty with arr.And it didnt work.So I thought somehow it may be mutating the global Array.And thus I tried out this way.
Perhaps its a bug.

Probably when you tried it the other way you had a small error that got incidentally fixed when you created arr2. That sort of thing happens a lot.

1 Like

@ArielLeslie I know when you read the following you would not believe.
But this happened…
I cut the required code…Reset the code…(thus all comments were deleted)…And pasted it again…
And Booom!It ran all the way!! :sweat_smile:

Ah. There you go. I bet that the test saw the this comment and thought it was part of the code:

//var globalArray = [5, 6, 3, 2, 9];
1 Like