7. Search for a friend


#1



Oops, try again. Did you create a function called search?


var friends = {
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "(206) 555-5554",
        address: ['Bertha De Dekenlaan','Aalst','WA','98052']},
     bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "(206) 555-5555",
        address: ['One Microsoft Way','Redmond','WA','98052']}
};

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

I don't get why it wont recognise my function search? Help please!

Kind regards


#2

if you where to add a function call:

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

you will get the following error:

ReferenceError: object is not defined

where do we have object, ah here:

for (var key in object)

object should the name of your object you want to loop over, which is?


#3

I want to loop over friends? Or name?


#4

Okay so I changed it to friends but now it doesn't only output Bill, it also outputs Steve's info. How comes that?


#5

i left that open for you to figure out what you needed to loop over

the exercise calls the function for validation purpose, because your function has console.log in it, this will show in the output window

nothing to worry about, run it in a free environment and this won't happen.


#6

Okay Thanks for your help!


#7