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 Data Using Objects The Build an Object Challenge, Part 2 Solution

Samuel Catchpole-Radford
Samuel Catchpole-Radford
8,081 Points

when i use exactly the same code as in the video, everything works but it only prints the last object to my page

So i have used exactly the same code in my own .js file and it only prints the last object in the array onto the page, i have 3 objects in the array but when the for loop runs it ignores the first 2 objects? any help?

2 Answers

rydavim
rydavim
18,814 Points

Without seeing your code, my guess is that you're using innerHTML and overwriting it for each element in your array. Does it work if you add everything to a variable like message and then print the variable?

If that doesn't help, you'll be able to get a more specific answer if you post your code.

Samuel Catchpole-Radford
Samuel Catchpole-Radford
8,081 Points

Thanks, i fixed it now thankfully, after ages looking at the code i realised i wasn't using += for my first message variable after setting it at the top of the .js file. I changed it and all worked ok.

rydavim
rydavim
18,814 Points

That would do it! Glad you got it sorted. :)