JavaScript JavaScript Arrays Multidimensional Arrays Create a Multidimensional Array

Kord Monaco
Kord Monaco
3,442 Points

Console gives me an error at line 14: Cannot read property '0' of undefined at createListItems

const playlist = [
  ['So What', 'Miles Davis', '9:04']
  ['Respect', 'Aretha Franklin', '2:45'],
  ['What a Wonderful World', 'Louis Armstrong', '2:21'],
  ['At Last', 'Ella Fitzgerald', '4:18'],
  ['Three Little Birds', 'Bob Marley and the Wailers', '3:01'],
  ['The Way You Look Tonight', 'Frank Sinatra', '3:21']
];


function createListItems( arr ) {
  let items = '';
  for ( let i = 0; i < arr.length; i++ ) {
    items += `<li>${ arr[i][0] }, by ${ arr[i][1] } - ${ arr[i][2] }</li>`;
  }
  return items;
}

document.querySelector('main').innerHTML = `
  <ol>
    ${createListItems(playlist)}
  </ol>
`;

4 Answers

Kord Monaco
Kord Monaco
3,442 Points

Thank you for the help Cameron! I edited my previous comments to include the correct format for extra practice.

Kord Monaco
Kord Monaco
3,442 Points

I did not add a comma after the first array, which gave me an error on line 14. Don't you love debugging as a beginner?

const playlist = [
  ['So What', 'Miles Davis', '9:04'],    <---
  ['Respect', 'Aretha Franklin', '2:45'],
  ['What a Wonderful World', 'Louis Armstrong', '2:21'],
  ['At Last', 'Ella Fitzgerald', '4:18'],
  ['Three Little Birds', 'Bob Marley and the Wailers', '3:01'],
  ['The Way You Look Tonight', 'Frank Sinatra', '3:21']
];
Cameron Childres
Cameron Childres
Treehouse Moderator 11,695 Points

Glad you got it! That's a way too familiar pain.

Quick tip, use the "markdown cheatsheet" linked under the comment box to format your code next time for easier reading. Like this:

```javascript
code goes here
```

Thanks - I had the exact same problem!