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

Srikanth Srinivasan
Srikanth Srinivasan
2,741 Points

Why is my code printing it out multiple times?

Can someone please explain why code is printing out the message multiple times? It doesn't make sense to me.

https://w.trhou.se/1p5twxqx2w

1 Answer

Steven Parker
Steven Parker
229,732 Points

The "+=" operator adds on to a string, leaving what was there before. So each time through the loop, the current record is added to the end of everything that was written before, and then the whole thing is output again.

Moving the "let html = '';" statement inside the loop will allow it to start fresh each time, and output each student record only once.

Srikanth Srinivasan
Srikanth Srinivasan
2,741 Points

Legend! It totally helps to get a second pair of eyes, I completely missed that. Thanks once again, Steven!