Syntax Error: Unexpected Token


#1



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


Greetings, fellow coders! Having a bit of trouble with this last exercise. I've scanned through the forums and thought I had this all correct, but I'm getting an error:

Syntax Error: Unexpected token .

I've been looking at this for a while now, so I'm sure fresh eyes would help. Can anyone point out my mistake?

Thanks in advance!


var friends = new Object();

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

friends.bill.firstName = 'Bill';
friends.bill.lastName = 'Gates';
friends.bill.number = '555-5555';
friends.bill.address = ['1 Microsoft Way', 'Redmond', 'WA'];

friends.steve.firstName = 'Steve';
friends.steve.lastName = 'Jobs';
friends.steve.number = '555-5555';
friends.steve.address = ['1 Infinite Loop', 'Cupertino', 'CA'];

friends.david.firstName = 'David';
friends.david.lastName = 'Brown';
friends.david.number = '555-5555';
friends.david.address = ['1 Main Street', 'Columbus', 'OH'];

var list = function(friends) {
    for (var firstName in friends) {
        console.log(firstName);
    }
};

var search = function(name) {
    for(var firstName in friends) {
        if(name === friends[firstName].firstName) {
            console.log(friends[firstName, lastName, address, number]);
            return friends[firstName, lastName, address, number];
        }
    }
};


What is wrong with my code : Contact List Adding properties
#2

Try this. Notice that friends.bill is not a variable as it belongs to friends object. Same for friends.david and friends.steve.

var friends = new Object();

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

    friends.bill.firstName = 'Bill';
    friends.bill.lastName = 'Gates';
    friends.bill.number = '555-5555';
    friends.bill.address = ['1 Microsoft Way', 'Redmond', 'WA'];

    friends.steve.firstName = 'Steve';
    friends.steve.lastName = 'Jobs';
    friends.steve.number = '555-5555';
    friends.steve.address = ['1 Infinite Loop', 'Cupertino', 'CA'];

    friends.david.firstName = 'David';
    friends.david.lastName = 'Brown';
    friends.david.number = '555-5555';
    friends.david.address = ['1 Main Street', 'Columbus', 'OH'];

    var list = function(friends) {
        for (var x in friends) {
            console.log(x);
        }
    };

    list();

    var search = function(name) {
        for(var firstName in friends) {
            if(friends[firstName].firstName === name) {
                console.log(friends[firstName]);
                return friends[firstName];
            }
        }
    };
    search("steve");

Also try not to use same key and value name in the code like this:

It's very confusing instead use friends[x].firstName or friends[item].firstName or friends[key].firstName.
You can see and distinguish quickly which is your key and which is your value.

By the way, thanks for your problem as I did it myself with object literal notation now I got a chance to do this in your way(object constructor).


#3

Hi,

Please check if the logic I have given:

is correct or not. Although the code is working. I found a case where those seemed to be a variable and also can be the property of an object. Will let you know if I find good explanation.


#4

Thanks so much! It looks like calling them out as variables is what was throwing things off. Finally got that little green checkmark. Ready to move on! :grin: