I'm stuck!


#1

var friends = {};
friends.bill = {
    firstName: "Bill",
    lastName: "Gates",
    number: "1234567897",
    address: ["Washington", "DC", "20005"]
};
friends.steve = {
    firstName: "Steve",
    lastName: "Jobs",
    number: "9876543214",
    address: ["New York", "Empire State Building", "234"]
};
friends.alin = {
    firstName: "Alin",
    lastName: "Dinu",
    number: "4522329912",
    address: ["Mizil", "George Raneti", "43"]
};
var list = function(contactid) {
    for(var option in contactid) {
        console.log(option);
    }
};
var search = function(name) {
    for(var key in contactid) {
        if(friends[key].firstName === name) {
            console.log(friends[key]);
            return friends[key];
        }
    }
};

#2

What are you trying to do?


#3

I just can't understand this functions for objects


#4

That doesn't tell me what you want to know. Do you have a question?


#5

yes what this meansfriends[options].firstName that' s looking for a friends subarray/object which must have name "firstName"?
also I did'nt understand this syntaxfor(var options in friends)?


#6

I passed the checkpoint and didn't understand anything.


#7

friends[options].firstName

friends[options] is one of the persons
then, the firstName property is looked up for that person.

for(var options in friends)

Is a loop. You can read about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

friends = {
    bob: {
        firstName: 'Bob'
    }
}
console.log(friends['bob'].firstName)
// Output from the above print:
// Bob


for (var key in friends) {
    console.log("the current key is: " + key);
    friend = friends[key];
    console.log("the current friend object is: %o", friend);
    console.log("that friends' first name is: " + friend.firstName);
    console.log("%o", friends[key]);
}
// Output from the above loop (there's only one friend, so only one iteration)
// the current key is: bob
// the current friend object is:  Object { firstName: "Bob" }
// that friends' first name is: Bob

#8

ok but why is working only for firstName this if if(friends[options].firstName === name) {...and if we change it with lastName doesn't work?


#9

If you're looking for a first name then you'll have to use the firstName property

If you're looking for the last name, then compare to the last name

If you're looking for both, look at both


#10

for both the code must look something like if(friends[options].firstName === name && friends[options].lastName === name)?


#11

Do you really expect a person to have the same first and last name?


#12

so then i must input 2 parameters in a function?


#13

That doesn't say anything, those are just words


#14

You can do it in any way you want. It just has to add up to the result you're after. The interpreter does exactly as told, it won't make it right for you. Consider how you would do it manually, the code would be similar to that.