Search results (.7 Search for a Friend)


#1


Exercise 7 - Search for a friend


My codes 'passes' but the result does not make sense to me. I search for Larry and I get Larry plus Steve, Steve again, and Bill. I really do not understand this result.

Console output:
bill
steve
rick
larry
==========
{ firstName: 'Larry',
lastName: 'Colley',
number: '843-555-5858',
address: [ '1000 Hobcaw Pkwy', 'Mt Pleasant', 'SC', '29464' ] }
{ firstName: 'Steve',
lastName: 'Price',
number: '704-555-1212',
address: [ '6718 Sardis Rd.', 'Charlotte', 'NC', '28211' ] }
{ firstName: 'Steve',
lastName: 'Price',
number: '704-555-1212',
address: [ '6718 Sardis Rd.', 'Charlotte', 'NC', '28211' ] }
{ firstName: 'Bill',
lastName: 'Gates',
number: '206-555-5555',
address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }
{"firstName":"Larry","lastName":"Colley","number":"843-555-5858","address":["1000 Hobcaw Pkwy","Mt Pleasant","SC","29464"]}


var friends = {};
friends.bill = {
    firstName: 'Bill',
    lastName: 'Gates',
    number: '206-555-5555',
    address: ['One Microsoft Way','Redmond','WA','98052']
};
friends.steve = {
    firstName: 'Steve',
    lastName: 'Price',
    number: '704-555-1212',
    address: ['6718 Sardis Rd.','Charlotte','NC','28211']
};
friends.rick = {
    firstName: 'Rick',
    lastName: 'Strickland',
    number: '843-555-6789',
    address: ['1023 32nd Ave','Isle of Palms','SC','29451']
};
friends.larry = {
    firstName: 'Larry',
    lastName: 'Colley',
    number: '843-555-5858',
    address: ['1000 Hobcaw Pkwy','Mt Pleasant','SC','29464']
};

var list = function(obj) {
    for (var prop in obj) {
        console.log(prop);
    }
    console.log('==========');
};

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

list(friends);
search('Larry');


#2

The output of your code is:

bill
steve
rick
larry
==========
{ firstName: 'Larry',
  lastName: 'Colley',
  number: '843-555-5858',
  address: [ '1000 Hobcaw Pkwy', 'Mt Pleasant', 'SC', '29464' ] }

(Formatting of the last part varies between js interpreters)

Note that rather than printing from the function, you should print the result of the function:

console.log(search('Larry'));

The reason for this is that printing isn't part of the function's purpose, printing is often quite useless because other parts of the code can't obtain the result, and it pollutes the output if something isn't explicitly meant to be displayed


Any additional output shown isn't produced by the code you posted, so either different code and/or codecademy's tendency to print out the last value used in the code


#3

Thank you. I'm glad to see its working. The code I used was copied and pasted directly from codecademey's example of what we were supposed to write (see 12.1) So the extra output must be from codecademey's tendency to output last value used, as you say.


#4

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