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 trialEugene Paitoo
3,922 PointsTwo 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
Treehouse TeacherHi 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!
Eugene Paitoo
3,922 PointsYes, it does. Thanks!
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsAakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsHey 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
Treehouse TeacherJennifer Nordell
Treehouse TeacherAakash 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:
Happy coding!
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsAakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsThanks :) . I just used an extra variable
result
to have better understanding and to make code more cleaner.Is my approach right , that I created an new variable between two "for" loops? Is there any problem ?