Use the Spread Operator to Evaluate Arrays In-Place Query

Tell us what’s happening:

Can anyone explain why this isn’t passing the test? Is this method wrong?
Thanks! Luke

Your code so far


const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2 = [];
(function() {
  "use strict";
  arr2.splice(0, 0, ...arr1); // change this line
})();
console.log(arr2);

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-the-spread-operator-to-evaluate-arrays-in-place

One thing is that you don’t need splice or the anonymous function wrapper. Spreading the array unpacks it into a list, effectively, which can be used as the argument to creating another array.

What method am I meant to call to transfer the contents?

let arr2 = ...arr1;

Isn’t allowed.

Have a look at this,

1 Like

your code give the right result but i think that is not the way FCC wanted you to do the challenge.
you can try to do it without using splice method.

in your example

let arr2 = ...arr1;

that will throw an error because …arr1 can not be used alone.

think of like that.
the spread operator is a list of the items inside the array but it’s not an array itself.
if x = [1, 2 , 3]
…x here is 1,2,3 but it’s not an array you have to make it an array and that is possible without using any method
.

You can define an array by

var arr = []; // empty array
var arr = [6, 89, 3, 45];  // array populated by list
2 Likes

Yeah I’ve just got it! Thank you.

Happy to have helped - you’re welcome. :slight_smile:

Hi,
I’m facing similar problem with a different code. Any idea why it is not passing the test?

let arr2;
(function() {
  "use strict";
  arr2 = push(...arr1); // change this line
})();
console.log(arr2);

You need to push your elements to somewhere. You didn’t indicate where you want to push.