Codeadademy contact list building 7/8 ... Without returning contact information

I am doing a section of the codeacademy class "creating a contact list" .. what's wrong here? keep getting the error "Oops, try again. It looks like your search function is not returning contact information for Steve." ( http://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/7 )

var friends = {};
friends.bill = {
    firstName: "Bill",
    lastName: "Gates",
    number: "(206) 555-5555",
    address: ['One Microsoft Way', 'Redmond', 'WA', '98052']
};
friends.steve = {
    firstName: "Steve",
    lastName: "Jobs",
    number: "(556) 555-5555",
    address: ['178 martio', 'cocoa', 'CA', '95074']
};
var list = function(friends) {
    for (var key in friends) {
        console.log(key);
    }
};
var search = function(friends) {
    for (var key in friends) {
        if (friends[key].firstName === "Bill" || friends[key].firstName === "Steve") {
            console.log(friends[key]);
            return friends[key];
        } else {
            console.log("couldn't find them");
        }
    }
};

      

+3


source to share


2 answers


Error in search function:

The instructions tell you:

Define a function lookup that takes one argument, name. If the argument passed to the function matches any of the first friend names, it should register that console friend's contact information and return it.

In a nutshell, it asks you to create a function in which you specify the name of the person you are looking for, while you provide friends , which is also a global variable .

The aim of the exercises is that with:

search("steve");

      

you should get the result:

Object :
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(556) 555-5555',
  address: [ '178 martio', 'cocoa', 'CA', '95074' ] }

      

In your (current) search function, you will not get the result from a needle (search parameter), but from your own settings defined in your if condition:



if (friends[key].firstName === "Bill" || friends[key].firstName === "Steve")

      

Hence, we are going to do the following:

  • set name as parameter
  • loop global friends variable
  • check if there are friends [key] .firstName equal to the provided needle (name).
  • if so, we'll write it down and return it .

Together:

var search = function(name) { // <-- note the name instead of friends.
    for (var key in friends) {
        if (friends[key].firstName === name) { // <-- note that if
            console.log(friends[key]);
            return friends[key];
        } else {
            console.log("couldn't find them");
        }
    }
};

      

And you're done!

http://prntscr.com/7kth5t enter image description here

Okay, anyway, you were close to a solution. If you still have any issues or need any clarification, feel free to comment.

+5


source


Use for printing:

list(friends);

      



and for searching:

search(friends);

      

+1


source







All Articles