How to Avoid For Loops that Manipulate the DOM in Javascript