I don't understand how to find the length of a subarray in Javascript. Here is an example from an exercise:
var table = [
["Person", "Age", "City"],
["Sue", 22, "San Francisco"],
["Joe", 45, "Halifax"]
];
I have tried to print out the elements of the sub-arrays individually using these for loops:
for(person in table) {
for(var i = 0; i < table[person].length; i++);
console.log(table[person][i]);
}
but it seems that
table[person].length
is not valid syntax although
table.length
is valid and
table[person][i]
returns the element at the sub-index table_person_i
table[person].lengthis valid syntax. Why do you iterate over the array with afor...inloop? Using aforloop might fix your problem.for...infor arrays. See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. And only because Codecademy doesn't like your code doesn't mean it is syntactically invalid.table[person]withtable['person'].personis a variable containing values such as"0","1", etc. So you end up doingtable["0"].length, exactly matches the structure. It gets the length of the first element intable.