Using sort without hardcoding globalarr

Tell us what’s happening:

Can someone help me understand why my code for this solution isn’t working just so I am aware that I don’t make these types of mistakes in the future? I see that the global array should not be hardcoded but I’m not entirely sure what that means. Thank you!

Your code so far


var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
// Only change code below this line
var newArr = globalArray.slice(0,5)

newArr.sort(function(a, b) {
return a - b
})

return newArr

// Only change code above this line
}
nonMutatingSort(globalArray);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36.

Challenge: Return a Sorted Array Without Changing the Original Array

Link to the challenge:

What errors or failing tests are you seeing?

" nonMutatingSort(globalArray) should not be hard coded."

You are referencing an external global variable by name. You are not using the argument arr at all. You also should’t depend on knowing that the array contains 5 items. This is hard-coding for the known test case rather than building a reusable function.

2 Likes