Search for a Friend


#1


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


My code doesn't seem to be working, and I thought I was following the instructions properly. What did I do wrong?

Oops, try again. It looks like your search function doesn't return contact information for Steve.


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "111-222-3456",
        address: ["Microsoft"]
        },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "234-345-3232",
        address: ["Heaven"]
        }
};
function list (friends) {
    for (var entries in friends){
       console.log (entries);
    } 
}
var search = function (parameter) {
  for (var search in friends) {
      return search;
  }
    
}


#2

So search should be iterating through the friends object. for ( var key in friends), ask if friends[key].firstName === parameter.

It would make more sense to change parameter to name, since that's what search is doing, but this is just a style thing. If the name matches friends[key].firstName, console.log(friends[key]), and also return it. I included passing code below, let me know if anything still doesn't make sense.

var friends = {
    bill: {firstName: 'Bill', lastName: 'Gates', number: '(206) 555-5555', address: ["Redmond"]},
    steve: {firstName: 'Steve', lastName: 'Jobs', number: '(420)-222-3333', address: ["Cuppertino"]},
};

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

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

#3

Maybe for a better understanding

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

T h e - B a s i c s

An object has one or more properties separated 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 separated 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] );


#4

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