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 Two-Dimensional Arrays

Michael Anetsberger
Michael Anetsberger
4,681 Points

Code not working -- No idea why

Here is my code... I keep looking at it and it just seems like I've got everything in place but when I preview it shows up without any list items at all

--

var playList = [
  ['I Did It My Way', 'Frank Sinatra'],
  ['Respect', 'Aretha Franklin'],
  ['Imagine', 'John Lennon'],
  ['Born to Run', 'Bruce Springsteen'],
  ['Louie Louie', 'The Kingsmen'],
  ['Maybellene', 'Chuck Berry'],
];

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

function printSongs(songs) {
  var listHTML = '<ol>';
  for ( var i = 0; i < songs.length; i += 1) {
    listHTML += '<li>' + songs[i][0] + ' by ' songs[i][1] + '</li>';

  }

  listHTML += '</ol>';
  print(listHTML);
}

printSongs(playList);
Michael Anetsberger
Michael Anetsberger
4,681 Points

I figured it out

   listHTML += '<li>' + songs[i][0] + ' by ' songs[i][1] + '</li>';

was missing a + between 'by' and songs[i][1]

a lesson in syntax for sure, it's incredible and pretty hilarious when you're stumped and the solution is as simple as missing one character.

Jay Gaulard
Jay Gaulard
13,739 Points

I was having an issue as well and realized that I was missing the commas after each array. I took a look at your code above and noticed that you added a comma after that last array: ['Maybellene', 'Chuck Berry'],. I tested both ways - once without the last comma and once with. Both worked fine, but I'm curious if something like that matters in the long run. Should you keep that last comma off or does it make no difference?

2 Answers

Michael Anetsberger
Michael Anetsberger
4,681 Points

Hey Jay Gaulard , the reason I've formed the habit of leaving a comma at the end is so that in the future if I ever need to add more to the array, I can do so without having to write a comma to start off so it's just quicker to get right to it. It doesn't serve any other purpose as far as I know and doesn't alter the program at all, it's a benign habit. I've just been told by some of my programming friends that it's best practice to do so.

Jay Gaulard
Jay Gaulard
13,739 Points

Thank you Michael! Nice tip.