Search for a friend. (can't get return Steve's number)


#1



Search for a friend


I've been dealing with this for few hours and losing my mind. It says my function doesn't return Steve's phone number but as you can see from my code it clearly should. As it is I'm getting Steve's and Bill's number in console. Where my code is wrong ? Thanks in advance ....


var friends = {

    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "07708071030",
        address: ['One Microsoft Qay']
        },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "0880989444",
        address: ['one infinite loop way']
        }

};

var search = function(name) {
    for (var firstName in friends) {
     
    if (name === "Steve") {
        console.log("0880989444");
        var stevePhone = "0880989444";
        return stevePhone;
    } else {
        console.log("07708071030");
        var billPhone = "07708071030";
        return billPhone;
    }
    }  
};
search("Steve");


#2

@markiscoding,
You will have to use a FOR-IN-loop
with which you traverse through the friends object....

+++++ nyc object and the for-in loop explained ++++++++++++++++

T h e - B a s i c s

An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it's associated VALUE

var nyc = {
       fullName: "New York City",
       mayor: "Bill de Blasio",
       population: 8000000,
       boroughs: 5
      };
nyc -object- S p e c i f i e d

The nyc object has 4 properties seperated by a comma-,
- a fullName property with property-key fullName and it's associated string value of "New York City"
- a mayor property with property-key mayor and it's associated string value of "Bill de Blasio"
- a population property with property-key population and it's associated number value 8000000
- a boroughs property with property-key boroughs and it's associated number value 5

for - in - loop - e x p l a i n e d

With the for-in-loop you have a Method
which will iterate over all properties of a given object.
At each iteration it will assign the property-key as a string
to a variable name of your choice.
Thus for (var x in nyc)
will lead to 4 iteration's
iteration-1 var x = "fullName";
iteration-2 var x = "mayor";
iteration-3 var x = "population";
iteration-4 var x = "boroughs";
As they want you to display the property-key and NOT it's associated VALUE
you would use
console.log( x );

If they wanted you to display the associated VALUE of the propery-key you would use
console.log( nyc[x] );


#3

This method has a huge drawback, if you now add a third person to your friends object, your search is not going to work. Take a look at this code:

var search = function(name) {
    for (var firstName in friends) {
        console.log(firstName)
    }
}

The for loop you created gives you the first firstName, so then you can use this to compare:

var search = function(name) {
    for (var firstName in friends) {
        if (firsName == "Bill"){
            console.log(friends)
        }
    }
}

now friends will be Bill, so now you can use this to return whatever is required to return


#4

@markiscoding,

var friends= {
              bill: { firstName: 'Bill',
                      lastName: 'Gates',
                      number: '444-444-4444',
                      address: [ 'One Microsoft way', 'Redwood', 'WA' ]
              },
             steve: {
                      firstName: 'Steve',
                      lastName: 'Jobs',
                      number: '333-333-3333',
                      address: [ 'Two infinite loop', 'Bentown', 'DV' ] 
              }
   };

keep repeating these two lines
An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it's associated value

Description of the friends object.
The friends object has 2 properties and are seperated by a comma-,

there is a bill property with property-key bill and it's associated object value
(this associated object has 4 properties, the property-keys being firstName, lastName,number and address)
there is a steve property with property-key steve and it's associated object value
(this associated object has 4 properties, the property-keys being firstName, lastName,number and address)

access via dot-notation == only literal property-key allowed

friends.bill ==> you will get the associated value of the bill property-key, thus
you get the object with 4 properties

friends.bill.lastName ==> you will get the associated value of the lastName property-key

access via the square-bracket-notation == literal & VARIABLE

1 using the literal property-key

friends["bill"]
friends["bill"]["lastName"]

2 using the property-key by reference (=== via a variable )

var propertyKey1 = "bill";
var propertyKey2 ="lastName";
friends[propertyKey1] ==> you will get the associated value of the bill property-key, thus
you get the object with 4 properties

friends[propertyKey1][propertyKey2]
in this case you will get the string VALUE of the lastName property
of the associated object VALUE of bill