Not the right friend's info


#1


I tried to print out Bill's information, I get Steve's. I tried to change the order of the info in the object and the first contact info is always the one printed, can you tell me what's wrong with my code?



The exercice is validated but I don't understand the results I get.
{ firstName: 'Steve',
lastName: 'Irwin',
number: '(323) 323 - 1234',
address: [ '213 Fake Street', 'San Pedro', 'CA', 90731 ] }
{ firstName: 'Steve',
lastName: 'Irwin',
number: '(323) 323 - 1234',
address: [ '213 Fake Street', 'San Pedro', 'CA', 90731 ] }
{"firstName":"Steve","lastName":"Irwin","number":"(323) 323 - 1234","address":["213 Fake Street","San Pedro","CA",90731]}


var friends = {
steve: {
        firstName: 'Steve',
        lastName: 'Irwin',
        number: '(323) 323 - 1234',
        address: ['213 Fake Street', 'San Pedro', 'CA', 90731]
        },
bill: {
        firstName: 'Bill',
        lastName: 'Nye',
        number: '(213) 213 - 1234',
        address: ['123 Fake Street', 'Los Angeles', 'CA', 90019]
}
};

var list = function(friends) {
for(var key in friends) {
    console.log(friends[key].address);
}
};

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


#2

Your search function either returns or prints in each iteration.
Should it really do either of those for non-matches?
What should it do for matches? Should it print or return? Doing both is really weird, functions should do one thing

So, a little more purpose is required. Decide/find out what it should do, do that and nothing else


#3

I found the solution thanks


#4

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