Search for a friend


#1

Not sure what went wrong. However, in the code below, where I inserted the "/* */" for the search code to be ignored. If I swap that block code for the one above it. It gives me an error message: "Oops, try again. Did you create a function called search?"

I cannot figure out what the difference between the two is.

Can anyone help?

Thanks in advance.

fyi: the code works as it stands. I am just trying to understand what is the difference in the two blocks of code.


var friends = new Object ();

friends.bill = new Object ();
    friends.bill.firstName = 'Bill';
    friends.bill.lastName = 'Gates';
    friends.bill.number = '(206) 555-5555';
    friends.bill.address = ['One Microsoft Way','Redmond','WA','98052'];
    
friends.steve = new Object ();
    friends.steve.firstName = 'Steve';
    friends.steve.lastName = 'Jobs';
    friends.steve.number = '(205) 666-6666';
    friends.steve.address = ['One Apple Way','Whitedale','CA','01620'];
    
friends.alvine = new Object ();
    friends.alvine.firstName = 'Alvine';
    friends.alvine.lastName = 'Walker';
    friends.alvine.number = '(302) 303-2030';
    friends.alvine.address = ['Seven Hoop Avenue','Glasskow','DE','19705'];
    
friends.mike = new Object ();
    friends.mike.firstName = 'Mike';
    friends.mike.lastName = 'Boggus';
    friends.mike.number = '(302) 222-1230';
    friends.mike.address = ['Ten Chase Lane','Newark','DE','18052'];

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];
    }
  }
};

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


#2

Also, if you please. How can I make my output look like this:

First Name: Steve
Last Name: Jobs
Number: (408) 555-5555
Address: 1 Infinite Loop
Cupertino, CA 95014

Instead of:

{ firstName: 'Steve',
lastName: 'Jobs',
number: '(205) 666-6666',
address: [ 'One Apple Way', 'Whitedale', 'CA', '01620' ] }
{"firstName":"Steve","lastName":"Jobs","number":"(205) 666-6666","address":["One Apple Way","Whitedale","CA","01620"]}

Thanks again.


#3

for(var key in freinds) {

friends*


#4

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