Contact List - Help!


#1

So no problem with the friends object, and I had success with the list function (but I still can't figure out why friends[key] prints out the two main properties...still figuring that out).

I'm running into an issue with the last function, search, as it outputs the following. Note that it outputs the steve object twice, and has the else output before each printed steve object. Stumped on this one. Would I need to do console.log(friends[key].contact)?

Contact not found.
{ firstName: 'Steve',
lastName: 'Irwin',
number: -3528,
address: [ 666, 'Venomous Terr', 'Brisbane', 'AUS', 54221 ] }
Contact not found.
{ firstName: 'Steve',
lastName: 'Irwin',
number: -3528,
address: [ 666, 'Venomous Terr', 'Brisbane', 'AUS', 54221 ] }
{ firstName: 'Bill',
lastName: 'Nye',
number: -1690,
address: [ 420, 'Science Drive', 'New York', 'NY', 11001 ] }
...

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Nye",
        number: 523-955-1258,
        address: [420, "Science Drive", "New York", "NY", 11001]
        },
    steve: {
        firstName: "Steve",
        lastName: "Irwin",
        number: 724-587-3665,
        address: [666, "Venomous Terr", "Brisbane", "AUS", 54221]
    
        }
};

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

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

#2

The second-output is caused by the code-checker-software...
to make sure
expand your code with

console.log('End of my code');


#3

For some reason I chose to return AND log to the console. I removed the console.log(), and it only prints once.

Another problem I found: putting hyphens between the phone number digits only return the final hyphen and the following numbers (see my code above). I don't know why it would let me print that as a number to begin with, but I have since removed the hyphens in all phone numbers.

I think I misread the prompt at asking for the values of firstName and number, but it just wants to print out all properties associates with the object that firstName belongs to. I am now successfully printing the chosen friend's contact information when using the search function!


#4

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