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!

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 Create an Array of Objects

Nicholas Wallen
Nicholas Wallen
12,278 Points

Not sure why the total price isn't calculating...


I have it copied word for work what the instructor had. But mine is calculating some crazy random numbers. Thoughts?

1 Answer

Emmanuel C
Emmanuel C
10,636 Points


Looking through your code, I noticed that the totalValues variable in your totalValue function is initialized to an empty array instead of a 0 like the instructor had it, which was causing all sorts of funkyness. After setting it to 0 the value returned was the same that the instructor had. So that should do it.

28,552 Points

This is correct, for those curious the reason that the "funkyness" occurs is that the += operator can only be used on int and string values. Using it on a type it is not compatible with results in JavaScript converting the value to a string.

So the variable gets converted from a list to a string once the += operator runs the first time, and from there it just has numbers concatenated to it. That's why you end up with such a long number, all of the prices are just glued together in a line instead of actually being added together mathematically.