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

BRENDAN MORRELL
BRENDAN MORRELL
3,626 Points

I cannot access the second layer of the array (e.g. the artist).

/*My code works when I just have songs[i] but then stops working if I use songs[i][0] or songs[i][1]

Here is the code:*/

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][0] + '</li>'; } listHTML += '</ol>'; print(listHTML); }

printSongs(playList);

Steven Parker
Steven Parker
229,644 Points

See the Markdown Cheatsheet link at the bottom of the "Add an Answer" section for instructions on how to format posted code. :arrow_heading_down:

2 Answers

Steven Parker
Steven Parker
229,644 Points

I see two issues:

  ["born to run", "bruce springsteen"]["louie louie", "the kingsmen"],   // you have this ...
  ["born to run", "bruce springsteen"], ["louie louie", "the kingsmen"], // ...instead of THIS

ā€” and ā€”

    listHTML += "<li>" + songs[i][0] + " by " + songs[i][0] + "</li>";  // you have this ...
    listHTML += "<li>" + songs[i][0] + " by " + songs[i][1] + "</li>";  // ...instead of THIS
BRENDAN MORRELL
BRENDAN MORRELL
3,626 Points

I'm not sure if this was what you were referring to, but in evaluating your response, I found the answer.

I was missing a single comma between ["born to run", "bruce springsteen"] and ["louie louie", "the kingsmen"]. However, my original code did not include those parentheses you added, so I'm not sure where those came from.

Regardless, thanks for helping me locate the issue.

Steven Parker
Steven Parker
229,644 Points

About the parens, I did a cut-and-paste but I did pass it through a "pretty print" to restore the formatting, perhaps it added them because of the missing comma.

Christopher Debove
PLUS
Christopher Debove
Courses Plus Student 18,373 Points

In your loop, if you want to target the artist, then you need to select the item at index 1. songs[i][1]

BRENDAN MORRELL
BRENDAN MORRELL
3,626 Points

I'm not sure if I'm misunderstanding you or if you're misunderstanding me: I am trying to access the song first, then add the word 'by' and then access the artist. However, I can't access anything but the full argument within the main array (e.g. I can type songs[i] and it will give me the song and the artist ( for example 'I did it my way, frank sinatra')) but if I add the second layer, and type songs[i][0] OR songs[i][1], neither outputs anything and the screen is blank

Basically, when I type:

songs[i]

It works, but if I type:

songs[i][1] OR songs [i][0]

both fail to work

BRENDAN MORRELL
BRENDAN MORRELL
3,626 Points

found the issue: there is a comma missing between ... 'bruce springsteen'] and ['louie louie'...