7/8 Code gets me through exercise but still have an error


#1

Here is my code for this 7/8

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

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

    else
    {
        return false;
    }
}

}

var friends =
{
bill:
{
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ["375 Dick Street", "Quebec, Canada", "G6H 8J7"]
},

steve:
{
    firstName: "Steve",
    lastName: "Emmerson",
    number: "(209) 555-5555",
    address: ["458 Dick Street", "Quebec, Canada", "G6H 8J7"]
}

};
list(friends);
search("Bill");

What I get from it is twice the contact informations of Bill and an error saying : ReferenceError: bill is not defined

Not really sure whats wrong here since it gets me through the exercise.

Thanks in advance!


#2

I think the problem is in your search function. The code still works, but putting in the line "return friends[bill]" gives the error because bill isn't ever entered in as a variable, but as a string.

"return friends[key]" works, however, because the variable key was defined as a variable in your for loop as "var key in friends".

Basically, just replace [bill] with [key] in the search function, and the code should work as normal.

Hope this helps!