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

Nour El-din El-helw
Nour El-din El-helw
8,241 Points

.join() isn't working

when i run the program using node gets me an error and pint to the dot between the array and 'join()' method. Here is my code

var products = [
  { name : 'pc', inventory : 5, unit_price : 45.99 },
  { name : 'iPhone', inventory : 7, unit_price : 40.99 },
  { name : 'iPad', inventory : 10, unit_price : 35.99 }
];

function listProducts(products_arr) {
    var productsNames = [];
    for ( var i = 0; i < products_arr.length; i++ ) {
      productsNames += products_arr[i].name;
    }
    return productsNames.join();
}

console.log(listProducts(products));
Nour El-din El-helw
Nour El-din El-helw
8,241 Points

Nevermind figured it out after watching the solution.Forgot that to add value(s) to an array you use 'push()' method.

1 Answer

Steven Parker
Steven Parker
231,269 Points

Apparently, using the "+=" operator to combine an empty array with a string converts it into a string.

And then "join" is not a valid method for a string.

Nour El-din El-helw
Nour El-din El-helw
8,241 Points

Yeah, I figured that out. If you see my comment on my question.Sometimes I forget some really basic stuff...;(

Steven Parker
Steven Parker
231,269 Points

...or you discover something pretty advanced!
I had no idea that combining a list with a string would produce a string — but now I do. :wink: