Lesson 7 - Search for a friend Need help


#1

Need help my code isn't returning steve contact information.


It displays both the names steve and bill but i keep getting this error - Oops, try again. It looks like your search function doesn't return contact information for Steve.


Replace this line with your code. 
var friends = {
       bill: {
            firstName: "Bill",
            lastName: "Gates",
            number: "12345",
            address: ['One Microsoft Way','Redmond','WA','98052']
            },
        steve: {
           firstName: "Steve",
            lastName: "Jobs",
            number: "1234576",
            address: ['Tne Microsoft Way','Bedmond','CA','98052']
            }
    };
    var list = function (friends) {
    for (var list in friends) {
        console.log(list)
    }
    };
    
    var search = function (friends) {
        for (var search in friends) {
            if(friends.firstName === friends) {
                console.log(friends[firstName]);
                return friends[firtName];
            }
        }
    };
    list(friends);
    search("Steve");


#2

you need to loop over friends which you defined here:

var friends = {
  // bill and steve
}

so giving your search function a parameter of friends is a bad idea. use something else, like names

then we loop over friends, which will give us bill and steve, which are both objects, so then we can do:

friends[search].firstName

to access the first names. then we should check if this equals names, which contains the first name we are searching for


#3

Sorry, but im not sure i understand correctly, where exactly i wanna loop and about what? >.< i made some changes to the code but still same error.

...

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "12345",
address: ['One Microsoft Way','Redmond','WA','98052']
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "1234576",
address: ['Tne Microsoft Way','Bedmond','CA','98052']
}
};
var list = function (friends) {
for (var list in friends) {
console.log(list)
}
};

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

...


#4

okay, look:

var search = function (name) {
    for (var search in friends) {
        console.log(search)
   }
}
search("Steve");

run this code, as you can see, search gives gives you bill and steve. which are objects. so if we want to access the bill and steve object, we need to do:

for (var search in friends) {
    console.log(friends[search]);
}

since the bill and steve object are nested inside the friends object. we use a loop (see code above) here to get all the objects inside the friend object, but we could also do it manually:

friends['steve']
friends['bill']

loop of course is a better solution, in particular if you are adding more friends. but showing how to do it manually will hopeful improve your understanding.

so to get the firstName manually, we could do:

friends['steve'].firstName
friends['bill'].firstName

or in case of the loop:

for (var search in friends) {
    console.log(friends[search].firstName);
}

now we could compare the firstName of each friend with name, since name contains the first name of the person we want to find

hope this helps


#5

Sorry i feel dumb now >.< but i dont know why its now workin >.< sorry

...

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

alternatively i also tried this

var search = function (name) {
for (var search in friends) {
if(friends.firstName === name) {
console.log(friends['steve']);
console.log(friends['bill'])
return (friends[search]);
}
}
};
list(friends);
search("steve")

...


#6

why don't you see you should do:

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

  }
}

i was hoping this would be clearer after my explanation


#7

sorry i had just got confused >.< and thank you for all your help and detailed explanation! :slight_smile: i will have more practice with objects :smile:


#8

it is confusing, because there is quit a lot happening here. normally when you access a property in object you can do:

example = {
  "property": "hello world",
}
console.log(example.property)

You can use a . to get access to a property, if you use . to access a property if it is the property name, if you want however to access the property with a string you need to do:

example = {
  "property": "hello world",
}
console.log(example['property'])

If you know where to use a loop:

example = {
  "property": "hello world",
}
for (properties in example){
   console.log(example[properties])
}

the value stored in the properties is a string, so you are forced to use [], you can't use ., it won't work, look:

example = {
  "property": "hello world",
}
for (properties in example){
   console.log(example.properties)
}

because properties contains a string. And then was just for a single object, in this exercise you have one object which contains multiply objects. Super confusing


#9

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


#10

This is suggestive of raw code that needs closer examination. One person may see this, and another will not. It's helpful to be explicit so that both are confident they see it.


#11

true, but that doesn't mean it deserves a flag? I wrote this explanation specifically for furryfoxash. I reckoned he was smart enough to see it. For other people who also see it, also nice, for people who don't, they can make there own topic with there own question.


#12

And I didn't suggest it did, which is why it was left for the member to explain. If they don't, then the flag can be deemed as unjustified. Wait and see.,