JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops The Refactor Challenge Solution

I put document.write(html) inside my for loop

Here is my solution. https://w.trhou.se/e7wwgfbezt

Kristaps Vecvagars
Kristaps Vecvagars
6,005 Points

Technically your code works, the result is the same, and you did nothing wrong, but... Putting document.write inside the loop means that every time a single lap of the loop runs, something is written in your html, and that can potentially slow down your program. Putting document.write outside the loop means that the program only writes to the page once, when the entire string is complete. In my opinion it's a more optimal approach.

Think of it this way - if you need 10 bottles of water, you can go to the store 10 times and bring back a bottle each time, but wouldn't it be more rational to go to the store once, stuff all 10 bottles in a bag and bring all 10 of them back at once?