Not sure if my code is 100% correct


#1

Hi,

In step 7 the instructions say:

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.

This my code

var friends = {};
    friends.bill = {
        firstName: 'Bill',
        lastName: 'Gates',
        number: '555-123-4567',
        address: ['1 Microsoft Road', 'Palo Alto', 'CA', '98765']
    };
    friends.steve = {
        firstName: 'Steve',
        lastName: 'Jobs',
        number: '555-123-4567',
        address: ['1 Apple Road', 'Cupertino', 'CA', '90210']
    };
    
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];
    }
    }
    };
    
list();
search();

However, if I put "Bill" or "Steve" in the search function, I'm still getting both entries and not just the one I searched for, my console output:

bill
steve
{ firstName: 'Steve',
lastName: 'Jobs',
number: '555-123-4567',
address: [ '1 Apple Road', 'Cupertino', 'CA', '90210' ] }
{ firstName: 'Steve',
lastName: 'Jobs',
number: '555-123-4567',
address: [ '1 Apple Road', 'Cupertino', 'CA', '90210' ] }
{ firstName: 'Steve',
lastName: 'Jobs',
number: '555-123-4567',
address: [ '1 Apple Road', 'Cupertino', 'CA', '90210' ] }
{ firstName: 'Bill',
lastName: 'Gates',
number: '555-123-4567',
address: [ '1 Microsoft Road', 'Palo Alto', 'CA', '98765' ] }
{"firstName":"Steve","lastName":"Jobs","number":"555-123-4567","address":["1 Apple Road","Cupertino","CA","90210"]}

Help? Thanks


How var key in Object works?
#2

your code is fine, don't forget you also call the list() function, which will list everyone.

I put your code in jsbin (which doesn't echo the last value to the console) and remove the list function (code), as you can see, only the friend you search for is returned. (press the run button in the top right)

So, your code is fine


#3

Thanks!

I have another question, I was having some issues figuring out the second bit of code for the search function and ended up copying it from the instructions:

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

I wasn't able to understand why in this case I had to use (friends[key].firstName === name) whereas in the first function I was just checking for (firstName in friends).

Can you please explain what "key" represents and why it is used in this case? Thanks


#5

name is the argument you pass into the function, you want to match this to the first name of one of your friends.

the variable key, you define here:

for (var key in friends)

this loop is doing all the work for you, it will loop over your object, which contains your friends, and at each run, will assign a friend to the key variable.

Let me explain better, we have an object friends:

var friends = {};

in this object, we create a new object (our friends, bill and steve), which you did in step 3

so, we have a object (friends) contain our actual friends (bill and steve), so the for .. in .. loop will now loop over our friends object, while it runs, the variable created in the for in loop (in your case key) will hold our friends. which we can see:

for(key in friends){
    console.log(key)
}

now, you called this variable key, but you could have called it anything. we can now use this key, to look what is inside our object:

for(key in friends){
    console.log(friends[key])
}

so now we use key, to look inside our friends object. Now, our object has several property's (firstName, lastName) which we can access, which is what you do in the search function. Hope this helps


#6

Thanks for mentioning jsbin, I will use it.


#7

Thanks, this makes sense. Appreciate the reply.