Tell us what’s happening:
I know I’m missing something obvious to complete this challenge, but with the code below, I keep getting the following error message:
“nonMutatingSort(globalArray) should return [2, 3, 5, 6, 9].”
Please can someone put me out of my misery and help me with the correct code?
Your code so far
var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
// Add your code below this line
var newArray = arr.concat(globalArray);
newArray.sort(function(a, b) {return a-b});
return newArray;
// Add your code above this line
}
nonMutatingSort(globalArray);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.
You are copying your array twice.
If you print newArray to the console, you will see the following array: [5, 6, 3, 2, 9, 5, 6, 3, 2, 9];
This causes your answer to be wrong. All you need to do is remove “globalArray” from your concat function.
var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
// Add your code below this line
var newArray = arr.concat();
return newArray.sort();
// Add your code above this line
}
nonMutatingSort(globalArray);
var globalArray = [5, 6, 3, 2, 9];
var temp = [];
function nonMutatingSort(arr) {
// Add your code below this line
var temp1 = temp.concat(globalArray);
temp1.sort(function(a,b){
return a-b;
});
// Add your code above this line
return temp1;
}
nonMutatingSort(globalArray);
var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
// Add your code below this line
let array = [].concat(arr)
array.sort()
return [].concat(array)
// Add your code above this line
}
nonMutatingSort(globalArray);
@conniepocky, @prasadbakare It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
Please focus on helping other campers with their questions and not just posting “your” full working solution.