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 Multiple Items with Arrays Using For Loops with Arrays

Joey B
Joey B
5,275 Points

Linebreaks and spacing in HTML source

When I preview the resulting code from this example, it prints out our list. But when I view the source and look at the HTML, the ordered list we created is formatted very nicely. I would think that our code would spit out a long, single line string..

Can anybody tell me where/when this formatting occurs? :) My best guess is that does print out a single line but it's Chrome that organizes it this way.. is that right?

function print(message) {
  document.write(message);
}

function printList (list) {
    var listHTML = '<ol>';
    for (i = 0; i < playList.length; i += 1){
        listHTML += '<li>' + list[i] + '</li>';
    }
    listHTML += '</ol>';
    print(listHTML);
}

printList(playList);

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

The default formatting for a list item means that lists are stacked on top of each other, just like any block level element. You can manipulate this with CSS if you wanted but it's default CSS values doing this not JavaScript! :-)

Matt Milburn
Matt Milburn
20,786 Points

Hi Joey,

Are you intending to print listHTML to the page as a string or a DOM element?

If you want to print it as a string and not a DOM element, you will need to use String.replace() to replace the < and > characters with &lt; and &gt;. Otherwise the string will be converted into a DOM element.