I’m a bit confused on this one. It appears that I get the correct results (I tried it with each of the test arrays given) and yet I’m still failing the exercise. Is there some reason why this might be happening?
First of all the first variable you defined does not change the array at all i.e. newArr1 is the same as arr1. But that isn’t why your code isn’t working.
A good way to debug and figure out why it’s not working is by using google dev tools>sources>New snippet.
If you run your code in the console you would notice that your code gives a nested array so you have to flatten it. An elegant method is using the spread operator by putting three dots (…) before the array that you want to flatten.
Hey @LDN - thanks for the help. After doing a bit of research I came across array.prototype.flat() and that seemed to solve most of the issue of the array flattening. I tried a few different methods of using the spread operator like you suggested, and I can’t figure out exactly where or what you mean. Could you please elaborate?
I wrote newArr1 to take a slice of arr1 which would change depending on the length to allow arrays with variable lengths to be passed, and assigned that to newArr1 so that arr1 was not mutated. I understand it’s not necessarily doing anything to modify the array, but the hope is that it would be variable that could accept any length thats input into the parameter.
The issue I’m still having is that its saying that arr2 is mutated after the script has run. I don’t quite understand why this is happening. Are any changes applied to newArr2 also being applied to arr2? If so, why is that the case?