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 trialNour 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));
1 Answer
Steven Parker
231,269 PointsApparently, 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
8,241 PointsYeah, I figured that out. If you see my comment on my question.Sometimes I forget some really basic stuff...;(
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.
Nour El-din El-helw
8,241 PointsNour El-din El-helw
8,241 PointsNevermind figured it out after watching the solution.Forgot that to add value(s) to an array you use 'push()' method.