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

David Dassau
David Dassau
8,628 Points

I can't get the songs and bands to print to the page

var playList = [ ['Dark Blue', 'Jacks Mannequin'], ['Master of Puppets', 'Metallica'], ['Sail', 'AWOLNATION'] ];

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

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

printSongs(playList);

3 Answers

andren
andren
28,558 Points

The issue is in this line:

for ( var i = 0; i < list.length; i += 1) {

You reference list.length but there is no variable called list in your script. I'm assuming you meant to type songs.length.

If you fix that typo like this:

var playList = [ ['Dark Blue', 'Jacks Mannequin'], ['Master of Puppets', 'Metallica'], ['Sail', 'AWOLNATION'] ];

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

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

printSongs(playList);

Then your code should work.

John Rugh
John Rugh
7,784 Points

Hi. I am stuck on this as well.

Here is the code:

var playList = [ ['I did it my way', 'Frank S'], ['Respect', 'Aretha F'], ['Imagine', 'John L'], ['Born to Run', 'Bruce S'], ['Louie Louie', 'The Kingsmen'] ['Maybel', 'Chuck B'] ];

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

And this is the error I am getting:

playlist.js:17 Uncaught TypeError: Cannot read property '0' of undefined at printSongs (playlist.js:17) at playlist.js:23

andren
andren
28,558 Points

There are two issues in your code:

  1. You are missing a comma after the second to last item in your playList array.

  2. You are missing an opening bracket for your for loop.

Here is the fixed code with some comments added:

var playList = [
    ['I did it my way', 'Frank S'],
    ['Respect', 'Aretha F'],
    ['Imagine', 'John L'],
    ['Born to Run', 'Bruce S'],
    ['Louie Louie', 'The Kingsmen'], // <- A comma was missing here
    ['Maybel', 'Chuck B']
];

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

function printSongs(songs) {
    var listHTML = '<ol>';
    for (var i = 0; i < songs.length; i += 1) { // <- This { opening bracket was missing here
        listHTML += '<li>' + songs[i][0] + ' by ' + songs[i][1] + '</li>';
    }
    listHTML += '</ol>';
    print(listHTML);
}

printSongs(playList);