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

Chelsey Machin
Chelsey Machin
2,901 Points

getting undefined error in loop: songs[i] is undefined

Hello, I've rewritten my code about 3 different times and still can't find my error. I'm hoping someone can take a look and maybe see what I'm doing wrong here. I've compared my code to the example code in the video, and don't see any differences. When I preview my code, it doesn't show any of the loop content, and when I look in my web console, it shows an error on line 17 of songs[i] being undefined. Any and all help is appreciated. Thanks!

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

1 Answer

You forgot a comma after 'Louie Louie' in the playList array:

var playList = [
  ['I Did It My Way', 'Frank Sinatra'],
  ['Respect', 'Aretha Franklin'],
  ['Imagine', 'John Lennon'],
  ['Born to Run', 'Bruce Springsteen'],
  ['Louie Louie', 'The Kingsmen'], // You forgot this comma here
  ['Maybellene', 'Chuck Berry']
];
Chelsey Machin
Chelsey Machin
2,901 Points

Oh my goodness what a silly mistake! Of course I was only rewriting my loop portion of the code, as I was convinced it was something there that I was doing wrong. Thank you so much!