Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Using For Loops with Arrays

Using For Loops with Arrays - My list is printing inline without format like in the video... Please Help!

Everything is working as it should, except that my list is printing as one long, inline, comma-separated list, rather than in an ordered list with formatting like shown in the video.

My list looks like this:

"Invisible Man,Into the Wild,Finding Alaska,Drop City,On The Road,Lord of the Flies"

I'm in Chrome and moved everything to Codepen. I copied and pasted the exact CSS from the Workspace and made sure my HTML was almost exactly the same, but I've included it below just in case.

1) This is a link to the project file I'm working with in Codepen:

https://codepen.io/alex-franklin/project/live/XWxdwg

2) My JavaScript:

var bookList = [ "Invisible Man", "Into the Wild", "Finding Alaska", "Drop City", "On The Road", "Lord of the Flies" ]; var html = ''

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

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

print(bookList);

3) My HTML

var bookList = [ "Invisible Man", "Into the Wild", "Finding Alaska", "Drop City", "On The Road", "Lord of the Flies" ]; var html = ''

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

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

print(bookList);

1 Answer

Hi alex,

Your code is being displayed this way because you passed your bookList array to the print() function. This will only display the array alone. You need to pass bookList to your new function printList() instead. This will turn your array into an ordered list. It's the small mistakes that get us!

printList(bookList);