What's wrong with this code?


#1

7.Search for a friend
(https://www.codecademy.com/en/courses/javascript-beginner-en-3bmfN/0/7?curriculum_id=506324b3a7dffd00020bf661#)

I get no errors but is keeps showing the message "Couldn't find it!" even when I put Bill or Steve as an input.!!
In the end it prints Steve's info twice and Bill's once but I don't think this has nothing to do with the inputs..

var friends=new Object();

friends.bill={};
friends.steve={};

friends.bill.firstName="Bill";
friends.bill.lastName="Gates";
friends.bill.number="(+30) 698 4578 123";
friends.bill.address=['Smyrnis','Kozani','Greece','30'];
friends.steve.firstName="Steve";
friends.steve.lastName="CaptainAmerica";
friends.steve.number="(+44) 987 6521 398";
friends.steve.address=['Valtadoron','Ioannina','Greece','4'];

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

var fname=prompt("Enter a First name.").toLowerCase();

var search=function(name)
{
    for(var firstName in friends)
    {
        if(friends.bill.firstName===name)
        {
            console.log(friends.bill);
            return(friends.bill);
        }
        else if(friends.steve.firstName===name)
        {
            console.log(friends.steve);
            return(friends.steve);
        }
        else
            console.log("Couldn't find it.!");
            
        if(confirm("Check for another name?"))  
            fname=prompt("Enter a name.");
        else
        {
            console.log("Ok.!Hope we helped!Bye!"); 
            return "End";
        }
    }
};
    
var info=search(fname);

#2

@javaace36796

Your code doesn't seem to fit what codeacademy's instruction.

You should first create a for loop that is going to iterate over the friends. If friends indexed by firstName with firstName property is equal to name, we log all its information in the console.

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