Combine Arrays with the Spread Operator

Combine Arrays with the Spread Operator
0

#1

Tell us what’s happening:
I don’t know what’s wrong.

Your code so far


function spreadOut() {
  let fragment = ['to', 'code'];
  let sentence =  ['learning', 'to', 'code', 'is', 'fun']; // change this line
  return sentence;
}

// do not change code below this line
console.log(spreadOut());

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/combine-arrays-with-the-spread-operator/


#2

In your sentence array, you need to use the spread operator to add content of fragment array. You can’t just hardcode ‘to’ and ‘code’…


#3

I should write that code:
function spreadOut() {
let fragment = [‘to’, ‘code’];
let sentence; // change this line
return sentence = [‘learning’, ‘to’, ‘code’, ‘is’, ‘fun’];
}

// do not change code below this line
console.log(spreadOut());


#4

At some point, trying to hard code the expected answer does not work. This is a great example of that. Look at the example of how they used the spread operator. It is a great example.

You must learn to follow the instructions. It says you must use the spread operator. None of the solutions you have attempted so far do that.


#5

Your reply is for first code right?My s econd code is right?


#6

Neither of your solutions are correct, because you are not using the spread operator. You are trying to hard code the answer which will not pass the challenge requirements.


#7

Can you write me a example of spread operator.


#8

The challenge has a perfect example of how to use the spread operator. Just read through the example and see how they did it.


#9

Yes but I can’t understand the example.


#10

What do you not understand about the example?


#11

How can I make my code same of example?


#12

If you want some more examples, read through the documentation for the spread operator.


#13

That don’t tell me nothing for how can I do the speadOut operator.


#14

Can I ask you something?>


#15

Of course it did. The example below shows how you can concatenate two arrays by using the spread operator.

var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
arr1 = [...arr1, ...arr2];

After the above code runs, arr1 is [0, 1, 2, 3, 4, 5], because on the right side of the = operator an literal array is define by the [ and then ]. Then arr1 is spread into its separate elements, followed by arr2 being spread into its separate elements with the comma in the middle separating the two sets of elements.

In the challenge you are working on. You have one predefined array named fragment which you will need to spread out (using the spread operator). For the other elements, you will need to separate them with commas as you normally would do for elements in an array.

If you do not understand anything I have said here, please ask more questions.


#16

You can always ask a question here on the forum.


#17

Yes…when write that code:
function spreadOut() {
let fragment = [‘to’, ‘code’];
let sentence; // change this line
return sentence = [“learning”, “to”, “code”, “is”, “fun”];
}

// do not change code below this line
console.log(spreadOut());

It tell me that spreadOut should return ["learning", "to", "code", "is", "fun"] is right.But you tell me it isn’t right.


#18

You function definitely returns the correct array. The problem is you did not follow the instructions which are asking you to create this array using the spread operator. That is why you are failing the second test. The test you are failing says *"The spreadOut function should utilize spread syntax"

You tried to hard code the correct array by writing the following line:

return sentence =["learning", "to", "code", "is", "fun"];

This line above does allow you to pass the first test, but since you did not use the spread operator you are not passing the second test.

You will not pass this challenge without using the spread operator to create the final array which should be assigned to the variable named sentence. The ONLY line you should be changing in the original code provided is the one with the comment change this line.

let sentence; // change this line
return sentence;

As you have already found out, you can not just write let sentence = [‘learning’, ‘to’, ‘code’, ‘is’, ‘fun’]; and pass all the tests.


#19

And what can I write in this code?


#20

At this point, I suggest you study the example given in the challenge, look over all the documentation for the spread operator I put in one of my previous replies above, and finally do some searches on Google for “examples of spread operator to joins arrays” and see if you find any more examples which will makes things clearer to you.

I have given you all the information you need to solve this challenge, but I am not going to write out the code for you, because then you would learn nothing.

I don’t mind answering other questions about how any of the examples work if you are confused by anything I have written or is written in the documentation provided, but giving you the solution is not going to happen. You are going to have to make the effort to figure this out. You are going to need to show me some code where you attempt to use the spread operator and then I can explain why it does or does not do what you expect.