Combine Arrays with the Spread Operator -- help

Combine Arrays with the Spread Operator -- help
0

#1

Tell us what’s happening:
The following is the last sentence in the explanation of this exercise. Could someone use these “traditional methods” and show me how it would have been done without the spread operator?

Using spread syntax, we have just achieved an operation that would have been more more complex and more verbose had we used traditional methods.

Your code so far


function spreadOut() {
  let fragment = ['to', 'code'];
  let sentence = ["learning", ...fragment, "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

Without using ES6, it would have looked something like this.

function spreadOut() {
  let fragment = ['to', 'code'];
  sentence = ["learning"];
  for(let index = 0; index < fragment.length; index++){
    sentence.push(fragment[index]);
  }
  sentence.push("is", "fun");
  return sentence;
}

A little bit more complicated, but you’ll save several lines of code if you use the spread operator.


#3

Another without using ES6 would have been:

function spreadOut() {
  let fragment = ['to', 'code'];
  let sentence = ["learning"].concat(fragment).concat("is", "fun"); 
  return sentence;
}