3. Adding your Friends (using the "constructor" method)


#1

So I am attempting to "Add my friends" to the newly created "friends" object. In the example, we see this done in Literal Java notation.
What I am trying to do is complete the task using the Constructor method.

Below is the code that I have written so far for my friends Bill and Steve:

When I try to run the code I get the following error:

SyntaxError: Invalid or unexpected token<<

https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/3?curriculum_id=506324b3a7dffd00020bf661

var friends = new Object();

var bill = new Object();
    bill.firstName = 'Bill';
    bill.lastName = 'Dufus';
    bill.phone = '800-347-9919';
    bill.address = '2 Enterprise Los Angeles, CA 92656';
    
var steve = new Object();
    steve.firstName = 'Steve';
    steve.lastName = 'Pear';
    steve.phone = '800-777-1313';
    steve.address = '337 Roadkill Way New York, NY 11217';

Any feedback would big super appreciated.

Many thanks!


#2

AAAAAAAAND I just figured out how to make the code run, but it doesn't seem right to me? Thoughts?

var friends = new Object();

friends.bill = new Object();
friends.steve = new Object();

var bill = new Object();
bill.firstName = 'Bill';
bill.lastName = 'Sosoft';
bill.phone = '800-347-9919';
bill.address = '2 Enterprise Los Angeles, CA 92656';

var steve = new Object();
steve.firstName = 'Steve';
steve.lastName = 'Pear';
steve.phone = '800-777-1313';
steve.address = '337 Roadkill Way New York, NY 11217';


#3

I see where your problem is occurring, or why it doesn't seem right. While the code you have will run it is just a little messy. You are declaring your objects of bill and Steve in your friends object when you declare
friends.bill = new Object(); so that in itself is declaring bill as a new object. You then give bill and steve properties under that code, while you declare them as new objects, which is not needed as you already declared them to be in the friends object, so you are creating other bill and Steve objects outside of your friends object. You can declare them as objects in the friends object and assign them properties as well with friends.steve = { }; I hope this helps you, happy coding :slight_smile:


#4

Eh, Thanks for taking the time to look at my code and respond. It's people like you that inspire me to learn and hopefully understand these new languages. Anyhow, I believe I implemented the changes that you suggested. Again, thank you for your suggestions.

var friends = new Object(); {

friends.bill = new Object(); {
friends.bill.firstName = 'Bill';
friends.bill.lastName = 'Sosoft';
friends.bill.number = '800-347-9919';
friends.bill.address = ['2 Enterprise ', 'Los Angeles ', 'CA', '92656'];
}

friends.steve = new Object(); {
friends.steve.firstName = 'Steve';
friends.steve.lastName = 'Pear';
friends.steve.number = '800-777-1313';
friends.steve.address = ['337 Roadkill Way ', 'New York ', 'NY', '11217'];
}
};


#5

Anytime man, I like to help because it lets me motivate (hopefully) and show that it is a big learning curve and your not in it alone :blush: Also, when you are declaring friends.bill there is no need to declare new Obejct(), when you place braces { } after your friends.bill it will interprate bill as an object because it is using literal notation to define bills properties at that point. Good luck friend :raised_hands:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.