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

Reviewing looping through a song list.

Good morning and Happy thanksgiving! I hope everyone is having a good holiday so far :). When someone has the time, can someone please review the code below? I'm trying to print out the contents of the array, but something is wrong with the code, because it's not processing what I'm asking it to do. I'm trying not to go back to look at the video.

var playList = [
  'Belong To The Music', 
  'Zelda Theme Music', 
  'Want You To Say', 
  'Peek A Boo'
];

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

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

printList(playList);

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Please review your printList() function - I see two issues:

  1. The <ol> declaration should be before the loop - you only want 1 ordered list - right?
  2. Why do you have a return statement? It stops the function and returns to the calling statement.

Oh, I see... I fixed it and it works fine now. Thanks.