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

listHTML does not get printed

HI guys,

I am struggliging with this code:

var playList = [ 'I Did It My Way', 'Respect', 'Imagine', 'Born to Run', 'Louie Louie', 'Maybellene' ];

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); }

printList(playList);

It seems that it does not print any html on my page. on the console I can't see any error message.

Is the syntax wrong? Thanks!

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

It's not the code. If I take your code and link it in to an html file it runs fine and displays the list on the page in an ordered list. So let's check some basics. Are you doing this in workspaces? Make sure you've saved your file and then preview it again and then refresh the browser. If this still doesn't work, try clearing your browser cache. Hope you get this fixed! :sparkles:

Thaks Jennifer, It was my workspace that was not working properly!

Adarsh Prabhu
Adarsh Prabhu
15,394 Points

My code does not work, although it is almost exactly in the format above

var theList = ['Goonies', 'Godfather', 'Conversation'];

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);
}

printList(theList);
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi Adarsh Prabhu! Well first, this probably should have been posted as an additional question. But the reason yours won't print is that you have a syntax error on this line:

listHTML += '<li>' + <list[i]> + '</li>';

If I change it to this, it runs:

listHTML += '<li>' + list[i] + '</li>';

The angle brackets are not needed around your reference to your array. :sparkles: