Spread operator without mutating elements ES6

1

I have to create a function that creates a new array based on the previous two but without mutating them.

function reverse(arr1, arr2) {
const arr3 = [...arr2, ...arr1];
return arr3; 
}

The problem is the following

 const arr1 = [1, 2];
 const arr2 = [3, 4];

 reverse(arr1, arr2);

 arr1.toEqual([1, 2]); 

};

I can not apply toEqual at arr1, I do not know if it is disarmed when I pass it to the reverse function and apply the spread, but if so, how can I create the new arrangement based on the other two, being that these remain arrangements?

    
asked by pedro 27.09.2018 в 22:50
source

1 answer

0

Your code is correct, you only need to assign the value that returns the function:

function reverse(arr1, arr2){
    return [...arr2,...arr1] 
}

const arr1 = [1, 2]
const arr1 = [3, 4]

const arr3 = reverse(arr1, arr2)
    
answered by 28.09.2018 в 00:00