Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Arrays Methods: Part 3

Victoria Rast
Victoria Rast
7,751 Points

Concatenation and joining in JavaScript Foundations Challenge

Hi, I am stumped on how to answer this code challenge question:

"On line 18 set the variable 'saying' to the concatenation of the 'first' and 'second' arrays with the word "dog". "

My code is below, I know how to concatenate and how to join, but how do I do them at the same time? I'm confused.

index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title> JavaScript Foundations: Arrays</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Arrays: Methods Part 2</h2>
    <script>
      var first =  ["The", "quick", "brown", "fox", "jumps"]
      var second =  ["over", "the", "lazy"];
      var saying = first.concat(second); 
      var shortSaying = saying;
      var sayingString = saying;
    </script>
  </body>
</html>

2 Answers

Colin Marshall
Colin Marshall
32,861 Points

You correctly joined the first and second arrays. You now need to add "dog" to the saying array.

You can do this simply by adding another argument to the concat method. Concat will take as many arguments as you give it and concatenate them all together.

var saying = first.concat(second, "dog");
Victoria Rast
Victoria Rast
7,751 Points

Thanks! It worked and it makes sense. :)

Jeff Jacobson-Swartfager
Jeff Jacobson-Swartfager
15,419 Points

The .concat() method can actually take multiple arguments. Each argument will be concatenated into the array .concat() is called on.

So, you can pass the second array and "dog" string as arguments to concat.

var saying = first.concat(second, "dog"); 

You can learn more about .concat() on MDN.