7. why isnt mine working


#1


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


var friends ={
    bill:{
        firstName: "Bill",
        lastName: "Asce",
        number: "404 918 5654",
        address:['2547 bradford square ne','atlanta','ga','30345']
    },

   steve:{
       firstName: "Steve",
       lastName: "Bhandari",
       number: "12345 664 4",
       address:['2551 bradford square ne','atlanta','ga','32659']
}
};

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


#2

friends is the global object. In this parameter we need a name.

var search = function (name) {

Now cycle through the friends object as you are, except with a generic local variable. key is quite commonly used in this instance since that is what we are iterating... keys.

    for (var key in friends) {

    }

Now there is no collision with the name variable. The if statement should not contain an else statement. Just the if.

        if (friends[key].firstName === name) {

        }

Lastly, we are asked to both log the object, and return it.

            console.log(friends[key]);
            return friends[key];

Piece this all together and study the result. It should begin to make sense. This exercise is more about mechanics than practical application. Study and practice this before moving on to the next unit. If need be, do this whole unit again just to reinforce the hidden lessons.


#3

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