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 Working with 'for' Loops Dynamically Display HTML with a Loop

1 Answer

Steven Parker
Steven Parker
229,982 Points

Your code produces items numbered 1 to 10, where the video code creates 0 to 9. Otherwise, the result is identical.

The video code builds up the "html" string in the loop first and then outputs it one time, where this code is outputting each element one at a time.

Also, since you're not assigning anything to the "html" variable you can eliminate it entirely:

for (i = 1; i < 11; i++) { main.innerHTML += `<div>${i}</div>`; }