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

I thought I followed the step. Why is my code wrong? Am I an idiot?

Can someone point out the obvious? How is this code wrong?

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

MOD NOTE: Formatted question to mark-down code for readability

2 Answers

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

based on a quick scan I would sat that you accidentally added a greater than sign instead of a semi-colon

//var listHTML = "<ol>">
var listHTML = "<ol>";
Dana Ng
Dana Ng
6,366 Points

I looked at his code, and I followed the code from the video, and I looked at LaVaugh's response. But I still cannot see why my code is incorrect...

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);
LaVaughn Haynes
LaVaughn Haynes
12,397 Points

I edited your comment to display the code correctly

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

Dana, See below:

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 ) {
    //single quote in wrong spot <-----
    //listHTML += '<li>' + list[i] + '</li'>;
    listHTML += '<li>' + list[i] + '</li>';
  }
  listHTML += '</ol>';
  print(listHTML);
  //extra curly bracket needs to be removed <-----
  //}  
}
printList(playList);
Dana Ng
Dana Ng
6,366 Points

Thanks LaVaughn, I didn't catch that extra curly bracket, but now I removed it it works!