7. Search for a friend, expected Steve, got Bill :)


#1


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


Hi, my problem is quite similar to this: https://discuss.codecademy.com/t/search-for-a-friend-error-it-looks-like-your-search-function-doesnt-return-the-contact-information-for-steve/47407

But not only does the code not return Steve's data, but it returns Bill's, that wasn't asked. Also I cannot see my first function work. Should I?

Error message: Oops, try again. It looks like your search function doesn't return contact information for Steve.

Thanks in advance for your assistance.

var friends = new Object();
    friends.bill = new Object();
        friends.bill.firstName = "Bill";
        friends.bill.lastName = "Nagy";
        friends.bill.number = "1234";
        friends.bill.address = ['1','Egyenes utca','Budapest'];
    friends.steve = new Object();
        friends.steve.firstName = "Steve";
        friends.steve.lastName = "Kiss";
        friends.steve.number = "4321"
        friends.steve.address = ['2','Ferde utca','Szolnok'];
    friends.petyu = new Object();
        friends.petyu.firstName = "Petyu";
        friends.petyu.lastName = "Kovacs";
        friends.petyu.number = "1324";
        friends.petyu.address = ['3','Cikkcakk utca','Debrecen'];
        
var list = function(friends) {
    for (var firstName in friends)
        {
        console.log(firstName);
        };
};

list();

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

search("Steve");


#2

Can we see your code?


#3

Holy cow, did I miss to paste it in... :confused: Sorry. It is filled with Hungarian names and places, never mind.


#4

See if this works for you

var friends = new Object();
    friends.bill = new Object();
        friends.bill.firstName = "Bill";
        friends.bill.lastName = "Nagy";
        friends.bill.number = "1234";
        friends.bill.address = ['1','Egyenes utca','Budapest'];
    friends.steve = new Object();
        friends.steve.firstName = "Steve";
        friends.steve.lastName = "Kiss";
        friends.steve.number = "4321"
        friends.steve.address = ['2','Ferde utca','Szolnok'];
    friends.petyu = new Object();
        friends.petyu.firstName = "Petyu";
        friends.petyu.lastName = "Kovacs";
        friends.petyu.number = "1324";
        friends.petyu.address = ['3','Cikkcakk utca','Debrecen'];
        
var list = function(object) {
    for (var property in object) {
        console.log(property);
    }
};

list(friends);

var search = function(name) {
    for (var neve in friends) {
        if (friends[neve].firstName === name) {
            return friends[neve];
        } else {
            console.log("No match!");
        }
    }
};

search("Steve");

#5

It returns this to me:

bill
steve
petyu
No match!
No match!

If I call the seacrh("Steve"); function, it returns:

bill
steve
petyu
No match!
No match!
No match!
{"firstName":"Steve","lastName":"Kiss","number":"4321","address":["2","Ferde utca","Szolnok"]}

  • which I don't quite understand why. Why "No match!" 3 times? And when you ask to
    return friends[neve];
    should it not return "Steve" simply?

#6

That's mine.

var friends = {
    bill: {
            firstName: "Bill",
            lastName: "Gates",
            number: "645-354-654",
            address: ["One Microsoft Way","Redmond","WA","98052"]
        },
    steve: {
            firstName: "Steve",
            lastName: "Jobs",
            number: "543-654-234",
            adress: ["One Apple Way","Richmond","WA","98052"]
        },
        
    andrew: {
            firstName: "Andrew",
            lastName: "Smith",
            number: "432-543-234",
            adress: ["One Tibam Way","Rurarlan","WA","98052"]
        }
};

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

list(friends);

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

console.log(search("Andrew"));
console.log(search("Bill"));

I think that this one is correct.


#7

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