7. Search for a friend


#1

var friends = {
    bill: {
        fristName:"Bill",
        lastName: "Gates",
        number: "12",
        address : ["One Avenue", "Back-to-the-Roots", "Jo"]
        },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "13",
        address: ["Two Avenue", "Forward-to-the-Bottom", "No"]
        }
    };
      var list = function(friends) {
            for(var key in friends) {
                console.log(key)
                }
            };
            
            var search = function(friends) {
                for(var key in friends) {
                    if(friends[key].firstName === "Steve") {
                        console.log(friends[steve]);
                        } else {
                            return "Friend was not found";
                            }
                    };
                };
                
                search("Steve");

I get "Friend was not found". Why I don't get the whole information of Steve? And what exactly do "var key in friends" exactly? I did understand, key is a new variable which is defined as a search-parameter in friends. But I don't understand why still I see "steve" and "bill" if I do console.log(key).

Thank you very much for your help :).


#2

set this argument to something relevant like fname or just name,

because here you are not supposed to check not for a specific string but just your name argument,

and then you would console.log that key not steve,

should be,

console.log(friends[key]);


#3

Hi Bandit,

Thank you very much for your response. I did change the argument, but I couldn't solve it, even I did put an other name and key:

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

        var search = function(fname) {
            for(var key in fname) {
                if(friends[key].firstName === "Steve") {
                    console.log(friends[key]);
                    } else {
                        return "Friend was not found";
                        }
                };
            };

            search("Steve");

#4

You misunderstood my instructions,

This line,

if(friends[key].firstName === "Steve") {

should reference fname not "Steve"


#5

take out the else part. the if else returns only the first key pair value in the object.


#6

Thx a lot, but I couldn't get that :/...I tried the example of Bandit and I copied the "solution" of part one, but even that doesn't function, I did replace prop to key:

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

search("Steve");


#7

Yeah I have the same code for the search function, and works on my end. Maybe post your whole updated code now?


#8

Hi Psdelarosa,

thx a lot:

var friends = {
    bill: {
        fristName:"Bill",
        lastName: "Gates",
        number: "12",
        address : ["One Avenue", "Back-to-the-Roots", "Jo"]
        },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "13",
        address: ["Two Avenue", "Forward-to-the-Bottom", "No"]
        }
    };
     var list = function(friends) {
            for(var key in friends) {
                console.log(key)
                }
            }
var search = function(fname) {
  for(var key in friends) {
    if(friends[key].firstName === fname) {
      console.log(friends[key]);
    }
  }
};

search("Bill");

#9

you took out the return, why?

from the instructions:

it should log that friend's contact information to the console and return it

you log to the console, but the return is now missing


#10

I put it, but then I get both:

Friend not found

and

information of Bill or steve...


#11

if i look at your code:

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

search("Bill");

i don't see return?

After you make changes, please include your full code again. We can't guess what you have done


#12

I put return and replaced with "friend not found".

var friends = {
bill: {
fristName:"Bill",
lastName: "Gates",
number: "12",
address : ["One Avenue", "Back-to-the-Roots", "Jo"]
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "13",
address: ["Two Avenue", "Forward-to-the-Bottom", "No"]
}
};
var list = function(friends) {
for(var key in friends) {
console.log(key)
}
}
var search = function(fname) {
for(var key in friends) {
if(friends[key].firstName === fname) {
console.log(friends[key]);
return "friend not found";
}
}
};

search("Steve");


#13

what this instructions mean is that you both need to log contact information and return the contact information. Don't return Not found, return the contact information


#14

Typo on the bill object. Says "fristname"


#15

I think that should work, along with stetim94's correction on return


#16

If I did not missunderstand that then I have to put instead of "friend not found" -> "friends[key]" right? I tried and didn't work well:

var friends = {
    bill: {
        fristName:"Bill",
        lastName: "Gates",
        number: "12",
        address : ["One Avenue", "Back-to-the-Roots", "Jo"]
        },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "13",
        address: ["Two Avenue", "Forward-to-the-Bottom", "No"]
        }
    };
     var list = function(friends) {
            for(var key in friends) {
                console.log(key)
                }
            }
var search = function(fname) {
  for(var key in friends) {
    if(friends[key].firstName === fname) {
      console.log(friends[key]);
      return friends[key];
    }
  }
};

search("Steve");

#17

You have a typo on the bill object - fristName - correct that to firstName: "Bill"


#18

I already did firstName: "Bill" :

var friends = {
bill: {
fristName:"Bill",

...


#19

you misspelled firstName, please correct the typo


#20

Yeah wrong spelling on firstName. you typed 'fristName'