Question 5 Tossing in an array


#1

I was exploring with the different ways to do this, don't quite get why the following code works just fine in Question 4, but somehow falls apart when adding in the two arrays:

var friends = new Object();
friends["bill"] = new Object();
friends.steve = new Object();

friends.bill.firstName = "Bill";
friends.bill.lastName = "Gates";
friends.bill.number = "1 987 382-5555";
friends.bill.address = ["One Microsoft Way", "Redmond", "WA", "98052"];

friends["steve"["firstName"]] = "Steve";
friends["steve".lastName] = "Ballmer";
friends.steve.number = "1 324 433-2394";
friends.steve.address = ["Two Microsoft Way","Redmond","CA","98100"];

console.log(friends.bill.address);
console.log(friends.steve.address);

The console logs the addresses as expected:
[ 'One Microsoft Way', 'Redmond', 'WA', '98052' ]
[ 'Two Microsoft Way', 'Redmond', 'WA', '98100' ]

But somehow I can't escape the error message:
Oops, try again.
Did you give each of your friends an address property?

What am I missing?


#2

You've got some syntax errors there so that shouldn't print anything at all.
So since your code won't run, it also won't create address properties for bill/steve


#3

So either the syntax errors worked on the prior exercise, or they are on the lines with friends.bill.address and friends.steve.address... care to say what they are?


#4

You have to be able to find your own mistakes.

If you temporarily delete half your code, then there's only half as much to look at. Repeat until obvious.


#5

Thank you, oracular black box. I already knew that all the bill syntax was fine, because it was the control. Hopefully the lesson will get to nested object syntax, because apparently javascript treats the properties in the form of obj["foo"]["bar"] and equivalently obj["foo"].bar... not as obj[foo[bar]] or obj[foo.bar]].

From what I've read, this is because javascript converts everything within the brackets to a string before looking up the reference, which means that you can use characters in the bracket notation that would be invalid in dot notation, such as object["!@#$%^&*()."] = true; (http://www.sitepoint.com/back-to-basics-javascript-object-syntax/)