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 trialH Yang
2,066 PointsBuilding a list
One mistake I made with this problem was that even though I made one-dimensional arrays to contain my correct and incorrect answers, I tried to call the data point from the array using a two-dimensional name- arr[i][0].
function printList (list) {
var listHTML = "<ol>";
for ( var i=0; i < list.length; i += 1) {
listHTML += "<li>" + list[i][0] + "</li>";
}
listHTML += "</ol>";
print(listHTML);
}
When I did this my buildList function spit out a list that looked like this:
1) H 2) H 3) H
The printList function worked normally when I called the information using list[i] instead of list[i][0].
What's going on with the function making a list of H's? And is it possible to call information out from two-dimensional arrays?
1 Answer
Jason Anello
Courses Plus Student 94,610 PointsHi Henry,
If your list is one-dimensional then it looks like each element in that array begins with an 'H'. Is that the case?
If it's an array of strings, then doing something like list[0][0] is going to give you the first character of the first string.
The first [0] will access the first string of the array. The second [0] will access the first character of that string.
console example:
> var list = ["hello", "goodbye"]
undefined
> list
["hello", "goodbye"]
> list[0]
"hello"
> list[0][0]
"h"
Yes, it's possible to get individual elements from 2 dimensional arrays.
The first index gets you one of the inner arrays and the second index gets you an element from that inner array.
H Yang
2,066 PointsH Yang
2,066 PointsWow you're completely correct- the first word of each string was "How", hence the list of "H's". Thank you again Jason.