Use the Spread Operator to Evaluate Arrays In-Place Query

Use the Spread Operator to Evaluate Arrays In-Place Query
0

#1

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


#2

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.


#3

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

let arr2 = ...arr1;

Isn’t allowed.


#4

Have a look at this,


#5

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
.


#6

You can define an array by

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

#7

Yeah I’ve just got it! Thank you.


#8

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