Not sure why my code doesn't return properly


#1



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


Error Message: It looks like your search function doesn't return contact information for Bill.


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


#2

Can you post the rest of the code?


#3

var friends = {
    bill: {
        firstName: "BIll",
        lastName: "Gates",
        number: "111",
        address: ["1", "Micro"]
    },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "222",
        address: ["2", "App"]
    }
};

var list = function () {
    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];
        }
    }
};

#4

Put "friends" in between the () after function, without the quotation marks of course.
Replace every part that says "key" with "firstName"


#5

Neither of those changes fixed the problem. I also don't understand why they would.

I think the problem is with the iteration with the for/in loop. I get this as the output in the console:

{ firstName: 'Steve', lastName: 'Jobs', number: '222', address: [ '2', 'App' ] }
{ firstName: 'Steve', lastName: 'Jobs', number: '222', address: [ '2', 'App' ] }

It should be showing bill's info first then steve's right?


#6

Well you need to make this an object for one thing. I'm working on the rest, but this should be:

var friends = new Object();


#7

Ok forget everthing I just told you. You made Bills name with a capital I in this section:

firstName: "BIll",
lastName: "Gates",
number: "111",
address: ["1", "Micro"]

Just change it to lowercase.


#8

WOW! thank you. It's funny how the smallest mistake can destroy everything


#9

Oh JawaScript WHY!!!