JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Two-Dimensional Arrays

Chelsey Machin
Chelsey Machin
1,101 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
1,101 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!