7.Search for a friend


#1

I checked my code with the example in part 1 and I can't find anything wrong. Here's my code:

var friends = {};
friends.bill = {
    firstName: "Bill",
    lastName: "Matts",
    number: "(222)222-2222",
    address: ['12345 Sicamore lane', 'Lincoln', 'California']
};
friends.steve = {
    firstName: "Steve",
    lastName: "Crane",
    number: "(333)333-3333",
    address: ['9807 Taco Place', 'Omaha', 'Nebraska']
};

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

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

This works for what it's supposed to, but the output isn't right it outputs the contact Steve 3 time and Bill 1 time. Can someone please help? Here's my output:

bill
steve
{ firstName: 'Steve',
  lastName: 'Crane',
  number: '(333)333-3333',
  address: [ '9807 Taco Place', 'Omaha', 'Nebraska' ] }
{ firstName: 'Steve',
  lastName: 'Crane',
  number: '(333)333-3333',
  address: [ '9807 Taco Place', 'Omaha', 'Nebraska' ] }
{ firstName: 'Steve',
  lastName: 'Crane',
  number: '(333)333-3333',
  address: [ '9807 Taco Place', 'Omaha', 'Nebraska' ] }
{ firstName: 'Bill',
  lastName: 'Matts',
  number: '(222)222-2222',
  address: [ '12345 Sicamore lane', 'Lincoln', 'California' ] }
{"firstName":"Steve","lastName":"Crane","number":"(333)333-3333","address":["9807 Taco Place","Omaha","Nebraska"]}

#2

@rednazer repost your code formatted. to do that do this below

```

post your code in between here


```

#3

I have the same problem! If I run my code outside of the platform It works fine inside codeacademy I get the output for steve and bill and the error


#4

I edited it for him :smile:


#5

You should still be passing it correct?

If it's just the output you are wondering about then that is the Correction Test that runs to check your code. That is causing the multiple outputs :smile:


#6

Yea it passes, but the output is completely messed up.


#7

Hi, I am having sort of the same problem but mine does NOT pass, here is my code:

var list= function(friends) {
for(var firstName in friends) {

    console.log(firstName);
}

};

var search= function(name) {
for( var key in friends) {
if(friends[key].firstName ===name) {
console.log(friends[key]);
return friends[key];
} else {
return ("I'm sorry this name does not exist");
}
}
};

search('Bill');

var friends= {
Bill: {
firstName: 'Bill',
lastName: 'Builder',
number: 07762678364,
address: ['7 moorlands RD', 'Camberley', 'Surrey']
},
Steve: {
firstName: 'Steve',
lastName: 'Wonder',
number: 07762678359,
address: ['Bombay Beach', 'Sydney', 'NSW']
},
Cristhian: {
firstName: 'Cristhian',
lastName: 'Gambobo',
number: 0127661946,
address: ['Wall St', 'TriBeCa', 'NY']
}
};

I call 'Bill' and i get the contact information for Bill repeated three times:

{ firstName: 'Bill',
lastName: 'Builder',
number: 7762678364,
address: [ '7 moorlands RD', 'Camberley', 'Surrey' ] }
{ firstName: 'Bill',
lastName: 'Builder',
number: 7762678364,
address: [ '7 moorlands RD', 'Camberley', 'Surrey' ] }
{"firstName":"Bill","lastName":"Builder","number":7762678364,"address":["7 moorlands RD","Camberley","Surrey"]}

However when i call 'Steve' it doesn't work at all, it just gives me Bills contact information again and it also returns my else {} response:

{ firstName: 'Bill',
lastName: 'Builder',
number: 7762678364,
address: [ '7 moorlands RD', 'Camberley', 'Surrey' ] }
"I'm sorry this name does not exist"


#9

Here is the perfect code, this worked for me, just return friends infomation out of the for loop

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

search("steve");


#10

I have the same issue. But after I delete the else part in for/in loops. It works. But still print out Steve 3 times....


#11

Just tried. But then no matter what I search for, it always output the first contact information in friends object.


#12

Code posted by @nomankarim has a flaw. We want to return value only if the condition of if is met:

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

search("steve");

#13

I meet same problem.
I use @nomankarim's code, but I can pass the course.


#14

I know that you can pass this exercise using his code. I just responded to the issue mentioned by @vibaobao:

no matter what I search for, it always output the first contact information in friends object

and now comes the question -> is codecademy about passing the exercises or about learning something? :slight_smile: Always try to check your code. If something is not working correctly -> fix it.


#15

Hi, thank you for your reply.
But if I put return back under if, then no matter what I search for, Steve will out twice and Bill once....
Any solution?


#16

This is the result of SCT (submission correctness test). Codecademy is executing your function to check if it works correctly. You can run your script in normal environment and you will see that you get only the input that you need, example ->

Code
var friends = {};
friends.bill = {
    firstName: "Bill",
    lastName: "Gates",
    number: "(o4)2323232",
    address: ["ulcia tamo vamo", "Texas", "34"]
};
friends.steve = {
    firstName: "Steve",
    lastName: "Jobs",
    number: "(04)32424242",
    address: ["Slepa ulica", "Florida", "12"]
};
friends.familiy = {
    firstName: "Familiy",
    lastName: "Keleman",
    number: "(04)2345467",
    address: ["ulica rudi", "Kranj", "30"]
};
var list = function (par) {
    for (var key in par) {
        console.log(key);
    }
};
var search = function(name) {
    for (var key in friends) {
        if (friends[key].firstName === name) {
            //console.log(friends[key]);
            return friends[key];
        }
    }
};

search("Steve");

#17

returning out of the loop is no efficient as it seems to store the first search result in memory, thus your next search result wont be efficient and just return what was already stored in memory outside the loop the first time.