Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Two dimensional arrays

Hello,

Why did we use an index of 1 for the artise . I thought arrays begin with an index of zero. listHTML += '<li>' + song[i][0] + ' by ' + song[i][1] + '</li>';

Thanks!!!

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're absolutely correct. Let's take another look at the play list:

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']
];

So this is an array that contains more arrays. If we were to say playlist[1][1], that would be equal to "Aretha Franklin". Each sub array of the main array contains two indexes. At index of 0 is the title of the song. The index of 1 contains the artist.

Hope this clarifies things! :sparkles:

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,625 Points

Hey Jennifer . What if there are 10 item in a single sub array ?
Do in that case too , we will use the same method or we will introduce another variable "j" for another internal loop.
If yes , then why doesn't he use another variable "j" here and use two loop . I think that will be suitable for all the cases , no what what is the length of the subarray.
I am trying to do that , but I am stuck . Please help me.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Aakash Srivastav Yes, that would be a great way to do it, especially if you don't know in advance how many elements there are in the subarray, although, that is not the case here. The problem, in this case, is the word "by". For this, we would have to include a conditional to so that we can say whether we should write that word or not.

Here was my solution using a nested loop:

var playList = [
    ['I Did It My Way', 'Frank' ],
    ['Respect', 'Aretha' ],
    ['Imagine', 'John' ],
    ['Born to Run', 'Bruce' ],
    ['Louie Louie', 'Kingsman' ],
    ['Maybellene' , 'Chuck' ]
    ];

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

    function printList( list ) {
     var listHTML = '<ol>';
     for ( var i = 0 ; i < list.length ; i++) {
         listHTML += '<li>';
       for( var j = 0 ; j < list[i].length ; j++){
            if(j == 0) {
                listHTML += list[i][j] + ' by ';
            } else {
                listHTML += list[i][j];
            }
       }
       listHTML += '</li>'
     }
     listHTML += '</ol>';
     print(listHTML);
    }

    printList(playList);

Happy coding! :sparkles:

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,625 Points

Thanks :) . I just used an extra variable result to have better understanding and to make code more cleaner.

var playList = [
['I Did It My Way', 'Frank' ],
['Respect', 'Aretha' ],
['Imagine', 'John' ],
['Born to Run', 'Bruce' ],
['Louie Louie', 'Kingsman' ],
['Maybellene' , 'Chuck' ]
];

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

function printList( list ) {
  var listHTML = '<ol>';
  for ( var i = 0 ; i < list.length ; i++) {
    var result = " ";
    for( var j = 0 ; j < list[i].length ; j++){
      if( j === 0){
       result += list[i][j] + " by ";
     }
     else{
      result += list[i][j];
    }

  }
  listHTML+= '<li>' + result + '</li>';
}
listHTML += '</ol>';
print(listHTML);
}

printList(playList);

Is my approach right , that I created an new variable between two "for" loops? Is there any problem ?

Yes, it does. Thanks!