I'm stuck with this code, please help


#1

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

Oops, try again. It looks like your search function doesn't return contact information for Steve.

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "jan",
        number: "5415614561",
        address: ["rosenheim","frühlingstr80","plz:83022"]
        },
    steve: {
        firstName: "Steve",
        lastName: "tomas",
        number: "5745454",
        address: ["vogtareuth","83569"]
        }
    };
var list = function(friends) {
    for (var property in friends) {
        console.log(property); 
            }
        }
    var search = function(name){
    for(var firstName in friends){
        if(friends.firstName === name){
            console.log(friends.firstName);
            return friends.firstName;
        }
    }
 };


#2

I would suggest having a different variable for your for loop because you would need to call it again in your if statement,

Let's say you write your for loop like this,

for (var key in friends) {

to check for a name in the contact list you would need a conditional statement that looks like this,

 if(friends[key].firstName === name) {

And to return the value of the list friends your return statement would look something like,

return friends[key];


#3

thank you so much for your help.

the code worked but it gives undefined output


#4

To fix that just change your console.log statement to,

console.log(friends[key)


#5

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