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

Two-dimensional arrays (JS), code isn't being shown

var playList = [
  [ "I Did It My Way", "Frank Sinatra" ],
  [ "Respect", "Aretha Franklin" ],
  [ "Put your Head on my Shoulder", "Paul Anka" ],
  [ "Imagine", "John Lennon" ]
  [ "Born to Run", "Bruce Springsteen" ],
  [ "Louie Louie", "The Kingsmen" ]
];

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

I have no idea why this code won't print out the function :( Any help would be lovely ! It says the error is around the for loop but I can't see what could be wrong

Moderator edited: Markdown added so that code renders properly in the forums.

1 Answer

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

Hi there! You're doing terrific. You really are. Would you believe that this all boils down to a single comma?

In your definition of playList you missed a comma:

  [ "Imagine", "John Lennon" ]

But that should be:

  [ "Imagine", "John Lennon" ],
// note the ending comma here

Because of the missing comma, the entire playList is invalid. Thus, when you later try to printSongs, it can't read that list and so, of course, can not read the properties of that list either :smiley:

Hope this helps! :sparkles:

edited for correction

Actually, that's not exactly the case. The first part of the playList is valid, but at that entry the value becomes undefined. Later on, in the for loop it is trying to access the zero and one index of undefined like ùndefined[0] and undefined[1]. Because that cannot be done, a syntax error is encountered and interpretation of the code is shut down entirely.

Thank you! Any idea why it was saying the error was in the loop and not in the two-dimensional array ? :)

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

Jack Camp If you remove the comma again and try and run it you'll see the error in the console and nothing will appear. But watch what happens when you type playList and retrieve the value of what's actually in that variable right now:

[Array(2), Array(2), Array(2), undefined, Array(2)]
0
:
(2) ["I Did It My Way", "Frank Sinatra"]
1
:
(2) ["Respect", "Aretha Franklin"]
2
:
(2) ["Put your Head on my Shoulder", "Paul Anka"]
3
:
undefined
4
:
(2) ["Louie Louie", "The Kingsmen"]
length
:
5

It does just fine right up until it gets to that one entry, but now that entry's value is very really ùndefined and it cannot read that. So it throws up a syntax error. Up until the for loop you're not trying to read the list so it doesn't matter until then :smiley:

On a side note, I added a clarification to my original answer about the playList being invalid. It's not that the entire playList becomes invalid, but rather introduces an element that can not be read in the same way as the others. When this happens interpretation of the code by the browser is shut down entirely.