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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Review Array Methods, Iterating and Two-Dimensional Arrays

Is the answer right/wrong to this question on the quiz on Review Array Methods, Iterating and Two-Dimensional Arrays?

On the quiz Review Array Methods, Iterating and Two-Dimensional Arrays from the course "JavaScript Loops, Arrays and Objects". <br> The question is: Finish the code by adding the array method that combines all of the items in an array into a single string. <br> var students = ["Sally", "Joan", "Raphael", "Sam"]; <br> var message = "Hello " + students._______(", "); <br> <br> I've entered toString like bellow: <br> var students = ["Sally", "Joan", "Raphael", "Sam"]; <br> var message = "Hello " + students.toString(", ");<br> <br> <hr> But the reply was that my answer was wrong. like bellow: <br> Bummer! Unfortunately, that answer is incorrect. <br> I'm 100% certain that I was right and I discovered a bug.

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Radiun,

While I would like to say you're right, sadly that's not true. The method the quiz is looking for is join which allows a custom delimiter to be set.

The reason it's not toString is because that doesn't allow for a custom delimiter and simply adds spaces between the array values.

Happy coding!

Yes, you are right. Thank you!

Shawn Flanigan
Shawn Flanigan
Courses Plus Student 15,815 Points

It's a bit confusing with this example. If you run the code as you (Radiun) have written it, it appears to do almost the same thing because toString puts commas between the array elements...albeit without spaces. If you try passing a different delimiter (like / or | ) into the methods, the difference becomes more obvious.

George Lazar
PLUS
George Lazar
Courses Plus Student 4,819 Points

Here's what worked for me:

var students = ["Sally", "Joan", "Raphael", "Sam"]; var message = "Hello " + students.join(", ");

You want to use the .join method. Here is the output from the Chrome console:

var students = ["Sally", "Joan", "Raphael", "Sam"]; var message = "Hello " + students.join(", "); console.log(message);

Hello Sally, Joan, Raphael, Sam VM257:3 undefined