Help....Not gettting proper output in 'Search for a friend'


#1


var friends = {
    bill:{
        firstName: "Bill",
        lastName: "Gates",
        number: "(206) 555-5555",
        address: ['One Microsoft Way','Redmond','WA','98052']
        },
    steve:{
        firstName: "Steve",
        lastName: "Jobs",
        number: "(408) 555-5555",
        address: ['1 Infinite Loop','Cupertino','CA','95014']
        }
    };
    var list = function (){
        for (var key in friends){
            console.log(key);
                }
        };
        function search(name){
            for( var key in friends){
             if(friends[key].firstName===name){
                console.log(friends[key]);          
                 return friends[key];
                 }  
            else {
             return "Name could not be found...";   
             }
             }
            
             }


Error message says 'search function doesn't return contact information for Steve'.


#2

There's a few extra closing braces but other than that the code looks ok though I'm not sure what the lesson's instructions are. Are you supposed to actually call your search function with "Steve" as the supplied argument?

Could you link the lesson instructions please?


#3

Thanks man....
The instruction is as follows...

"Define a function search that takes a single argument, name. If the argument passed to the function matches any of the first names in friends, it should log that friend's contact information to the console and return it."


#4

Can you link the page?


#5

Down below is the link:

https://www.codecademy.com/en/courses/javascript-beginner-en-3bmfN/0/7?curriculum_id=506324b3a7dffd00020bf661


#6

Is it passing for you now?


#7

No man...still getting the same error...

And regarding the extra braces you said earlier...I don't find any mistake in it..
Every braces are correctly organised...If not could you spot it please...


#8

Nevermind, I just didn't see it due to the tabbing. For some reason its your else clause. If you delete it the exercise passes.

Incidentally, it seems like given the path through this lesson block, the end solution should have had you use both functions together.

function list(obj) {
    for (var entry in obj) {
        console.log(entry, obj[entry]);
    }
}

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

#9

Awesome man!!!!!
It passed.......
Thanks


#10

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