7. Search for a friend


#1

Hi,

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

It says that:" Oops, try again. Did you create a function called search?" I wonder what is wrong with this.

var friends = {};
    friends.bill = {
        firstName: "Bill",
        lastName: "Hill",
        number: "050 750 77 88",
        address: ['Palo Alto Street', '9980', 'Palo Alto', 'CA']
        };
    friends.steve = {
        firstName: "Steve",
        lastName: "Jones",
        number: "070 770 88 99",
        address: ['Steve Jones Avenue', '9970', 'NY']
        };
    friends.jonna = {
        firstName: "Jonna",
        lastName: "Sansan",
        number: "090 990 77 88",
        address: ['Salmikankaantie', '700', '9900', 'Oulu']
        };
    friends.jenni = {
        firstName: "Jenni",
        lastName: "Kanto",
        number: "080 880 99 88",
        address: ['Kankaankuja 9', '90100 Oulu']
        };
    friends.senni = {
        firstName: "Senni",
        lastName: "Sammal",
        number: "050 750 88 98",
        address: ['Sennintie 700', '70750', 'Suomi']
        };
    friends.he = {
        firstName: "He",
        lastName: "Is",
        number: "090 770 30 35",
        address: ['Gatan', '600', '90750', 'Earth']
        };
        
        var list = function (friends) {
        }
        
        for (var firstname in friends){
            console.log(firstname);
            }
            
            var search = function (friends) {
                for (var [firstName] in friends) {if (key === firstName) {console.log(friends);}
                else {console.log("Your friend is not on the contact list.");}
                }}

#2

Your search function is wrong. As argument, you are giving it the array "friends", instead of some name. You should try to rewrite it yourself, if you don't know how let me know and I will provide you with solution and explanation.

Also, just a notice. You could have written your "friends" array like this:

var friends = {
    'bill' : {
        'firstName' : 'Bill',
        'lastName' : 'Gates',
        'number' : 'Windoze',
        'address' : ['One Microsoft Way','Redmond','WA','98052']
    },
    'steve' : {
        'firstName' : 'Steve',
        'lastName' : 'Jobs',
        'number' : 'Apples',
        'address' : ['Two Apple Way','Seattle','NY','12345']
    }
};

I think it's more practical then what you did, but it's just my opinion.


#3

It would be nice to hear the solution and explanation.


#4

Oh, I will post my search function and an explanation.
So, for start, here are the given instructions:

Instructions
Define a function search that takes a single argument, name. If the argument passed to the function matches any of the first names in friends, it should log that friend's contact information to the console and return it.

So, you should have taken a single argument "name" and give it to your function "search". Instead, what you did is sent whole friends array to your search function. That is wrong because if you give the whole array, then you don't know what exactly are you searching for. Is it someone named "Igor"? Or "Maria"? So, here is how search function should have looked:

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

This function I posted will take a single argument, we named it "name" (it could have been "firstName" or anything you want, but for this exercise it should be called "name").
Next, this function already have access to our friends array. To make function work with only given array, you could have sent another argument (so we'd had var search = function (name, arrayName) {...} for example, but here it is not needed since we'll only be working with friends array).

So, once you sent some name parameter to your function, it knows what to look for. It will go through each of the array elements (in this part - for(var key in friends) {...} ), and check if friends[key] ("key" is actually index of each of objects in our friends array) with firstName is equal to parameter name we sent to the function (so, it will take for example (from my previous post) key "bill" and check if friends[bill].firstName equals to something we sent to the function.
If we called function liked this:

// some code 
search("Bill");
// some code

In this case our result would be found, printed and returned for further usage.
If we called function like this:

// some code 
search("Igor");
// some code

Nothing would be found since we don't have element whose firstName property equals "Igor".

Please let me know if this is clearer now, if not I will try to find simpler example.


#5

Try this one friend

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "2734",
address: ["New York"]
},

steve: {
    firstName: "Steve",
    lastName: "Great",
    number:"2734",
    address:["Los Angeles"]
    }


};

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

 var search = function(name){
    for(var searching in friends){
    if(friends[searching].firstName === name)
    {

console.log(friends[searching]);
return friends[searching];
}

       }

    };

#6

This helped me, thanks! One minor thing though, you have an extra "=" equal sign on the third line of your example code:

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


#7

I'm glad it helped!

And actually, I used === on purpose there. It can give different result than ==, but the main difference that it wont do any type conversion (unlike ==), just compare variables and return true or false.
So long story short (about operations) is this:

=== operator behave identically to the == operators except no type conversion is done, and the types must be the same to be considered equal.

It's handy in most situations if you want to be 100% sure that you have exactly the result you expect in your variables.


#8

Hi,

When I write it says that: " Oops, try again. It looks like your search function doesn't return contact information for Steve."

var friends = {};
    friends.bill = {
        firstName: "Bill",
        lastName: "Hill",
        number: "050 750 77 88",
        address: ['Palo Alto Street', '9980', 'Palo Alto', 'CA']
        };
    friends.steve = {
        firstName: "Steve",
        lastName: "Jones",
        number: "070 770 88 99",
        address: ['Steve Jones Avenue', '9970', 'NY']
        };
    friends.jonna = {
        firstName: "Jonna",
        lastName: "Sansan",
        number: "090 990 77 88",
        address: ['Salmikankaantie', '700', '9900', 'Oulu']
        };
    friends.jenni = {
        firstName: "Jenni",
        lastName: "Kanto",
        number: "080 880 99 88",
        address: ['Kankaankuja 9', '90100 Oulu']
        };
    friends.senni = {
        firstName: "Senni",
        lastName: "Sammal",
        number: "050 750 88 98",
        address: ['Sennintie 700', '70750', 'Suomi']
        };
    friends.he = {
        firstName: "He",
        lastName: "Is",
        number: "090 770 30 35",
        address: ['Gatan', '600', '90750', 'Earth']
        };
        
        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];
                }
            }
            }
            
            search("Jenni");

#9

You should remove console.log() from search function, I've only put it if you're interested in data (I always love to inspect data and see why is it written etc.).

And you are not getting the right result because you written (inside search function) this:

// code
if (friends[key].firstname === name) {
// code

Instead of this:

// code
if (friends[key].firstName === name) {
// code

You don't have property firstname, but firstName (capital N).
So ideally, you'd want to call search function like this:

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

// output only friend that was found
console.log(search("Jenni"));

#10

@igorcaletacar Yeah, I actually saw the triple equal sign somewhere else right after I posted that reply, and looked into it on StackOverflow and realized it is a real thing. Of course I couldn't find this thread again afterwards to say sorry for correcting without looking into first. :frowning: Haha thanks for the explanation.