Search for a friend


#1

hi guys,

can you guys see what's wrong with my code, im trying to understand it as much as possible but its too choppy for me. it says that it doesn't return contact information for steve.

var friends = {
bill:{
firstName: 'Bill',
lastName: 'cyrus',
address: ['6 cow lane','Watford','Hertfordshire', 'WD8 2QD'],
number: 07778870778078
},
steve:{
firstName:'Steve',
lastName: 'knight',
address: ['8 Middle of nowhere', 'Birmingham', 'BH8 5TD'],
number: 18456451845641
}
};

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

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

#2

It's saying that because you need to go according to the exercise. Instead of Bill Cyrus do Bill Gates, instead of Steve Knight do Steve Jobs. Also put down' the correct address and numbers. Make sure you spell everything right with proper capitalization.

If there are any other reference errors then just let me know :slight_smile:


#3

thank you, i have tried that and that doesnt work, i get another error message "SyntaxError: missing before statement" . my code looks like this now:

var friends = {};
bill:{
firstName: 'Bill',
lastName: 'Gates',
address: ['One Microsoft Way','Redmond','WA','98052'],
number: "(206) 555-5555"
};
steve:{
firstName:'Steve',
lastName: 'Jobs',
address: ['1 Infinite Loop','Cupertino','CA','95014'],
number: (408) 555-5555
};

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

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

#4

The closing curly bracket should wrap around steve and bill.

Do you think I could have a link to the exercise?


#5

yeah sure

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

ok ill give it a go


#6

Forget my last statement, it appears that I was mistaken :slight_smile:


#7

ok, what am i missing?


#8

put the number in quotation marks. I'm still looking for other small bugs it will take me a few minutes.


#9

Use dot notation saying:
`
bill.friends = {
Contact Information
};

`

Do the same thing for Steve.

I think that the main problems are here:

In your for loops then you are creating variables that are already created.

and

Try substituting these for different variables.


#10

@teraslayer58765 Here is a hint from the exercise for what your search function would look like:


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

Hope this helps to explain it, I know that it can be confusing :slight_smile:


#11

thank you for all the help but it still not working. ive changed my code so it looks like that example you sent but its still not working. the error says "SyntaxError: missing before statement"

var friends = { };
bill:{
firstName: 'Bill',
lastName: 'Gates',
address: ['One Microsoft Way','Redmond','WA','98052'],
number: "(206) 555-5555"
};
steve:{
firstName:'Steve',
lastName: 'Jobs',
address: ['1 Infinite Loop','Cupertino','CA','95014'],
number: "(408) 555-5555"
};

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

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

#12

Remember to create bill and steve by saying

friends.bill = {
contact information
};

Do the same thing for steve.


#13

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