Return a Sorted Array Without Changing the Original Array.help

Tell us what’s happening:
nonMutatingSort(globalArray) should return [2, 3, 5, 6, 9]
My exercise tell me that and this is wrong.Can you tell me where is my wrong?

Your code so far


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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/return-a-sorted-array-without-changing-the-original-array

You declared globalArray twice in your code

I see this part of the code appearing twice

var globalArray = [5,6,3,2,9];
function nonMutationSort(arr) {

I remove that code var globalArray = [5,6,3,2,9];
But again that nonMutatingSort(globalArray) should return [2, 3, 5, 6, 9]. is wrong

var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
// Add your code below this line
return globalArray.concat(arr).sort( (a,b) => a - b );

// Add your code above this line
}
nonMutatingSort(globalArray);

This is my new code but isn’t right

Where can I write that code you send me.

You need not mutate the global array just create a new array in function and then do all the operations on it and return it.
Just declare the new array and then push items of existing globalarray in it using concat.
Try solving now.

So we start with this…

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

function nonMutatingSort(arr) {
  // Add your code below this line

  // Add your code above this line
}

// Use this to call your function with the parameter 
// that you've already defined as globalArray
nonMutatingSort(globalArray);

In the actual function, you just have to return and modify arr with sort()

Your function should return the arr array with the sort() method applied to it.

answer will just be a single line of code.

What I exactly do because I on’t understand.Can you help me more?

var globalArray = [5, 6, 3, 2, 9];
function nonMutatingSort(arr) {
  // Add your code below this line
 
 
_`You must change the code here only.`_                        



  // Add your code above this line
}
nonMutatingSort(globalArray);

This won’t work. globalArray is a reference to a global array. Calling sort on it mutates that array - the fact it is being passed into a function makes no difference, it’s still exactly the same array: this is explicitly what the exercise is explaining.

It isn’t the same - globalArray references a specific location in memory, and if you call sort on it, it will sort the referenced globalArray. This is why the exercise says to use concat

@Klaudia concat creates a new array from an array + some value you put in as an argument to the concat function. So if you do

> var oldArray = [1,2,3]
> var newArray = [1,2,3].concat(oldArray)

oldArray will still be [1,2,3], and newArray will be [1,2,3,1,2,3].

Similarly, if you concat an array onto an empty array, it will just create a new array that looks the same as the original one:

> var oldArray = [1,2,3]
> var newArray = [].concat(oldArray)

// There are _not_ the same array:
> oldArray
[1,2,3]
> newArray
[1,2,3]

In this case, you are making a copy of oldArray

arr in the function is just an array that gets passed in, which could be globalArray or it could be any other array - dont reference globalArray inside the function, that is just something that gets used during the tests.

What you are supposed to be doing is, inside the function, making a copy of arr and then returning a sorted version of that copy

1 Like

@DanCouper

var array1 = [1,2,3];
var array2 = [].concat(array1);
array2[0] +=5;

Using concat(), will this deep copy the objects in the array1, or will the new array2 still reference the original objects? If you mutate them, will it impact the original array as well?

concat creates a new array. See

If you mutate something, that means it is changed, that’s the definition.

I understand concat creates a new array. I was wondering if the objects of the new array would be references to the objects in the original array? Or, would they be new separate objects and therefore changes on one object (in the new array or old array) would not change the object in the other array automatically.

Thanks again for any clarification.

No, it’s a new array. Any non mutating methods copy/create. If you have a refence to another object in the array, and you copy that reference, then that’s still a reference to the original object (you’re making a shallow copy), but you are creating a completely new object

@DanCouper, I appreciate your responses!

So, mutating the shallow copy of the object in the new array would then be reflected in the reference to that same object in the original array?

const someArr = [1,2,3];
const anotherArr = someArr.slice();
const andAnother = someArr.concat(anotherArr);
const anotherOne = andAnother.map(x => x + 1);
const yetAnotherOne = [...anotherOne];

So every one of those is a different array, there is no effect on the previous arrays.

arrayOfReferences = [someArray, anotherArray, andAnother, anotherOne, yetAnotherOne]

This is a collection of references to other arrays. If I do

arrayOfReferences[0].push(4);

This will mutate the original array someArray. This isn’t a terribly common thing to do though outside of say game programming, because it gets nightmarishly hard to keep track of what’s happening. Arrays of objects it’s much more common, but it’s still preferable to not do it, or even better just flatten everything to one-dimensional data as much as is possible.

hi ~
The point of the problem is that the concat function returns a new array.
Therefore, we do not refer to the existing arr.:hear_no_evil: