Combine Arrays with the Spread Operator

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

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:

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’…

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

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.

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

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.

Can you write me a example of spread operator.

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

Yes but I can’t understand the example.

What do you not understand about the example?

How can I make my code same of example?

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

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

Can I ask you something?>

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.

You can always ask a question here on the forum.

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

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

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.

And what can I write in this code?

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.