7, Debug Code, need help please and thank you!


#1


Exercise 7. Search for a friend


Error message says function doesn't search for 'Bill'. I don't believe I coded the search function correctly - and I'm not sure if the error is from earlier code or not even though I passed through previous exercises. Sorry for the long cut and paste code.


var friends = new Object();

friends.bill = new Object();
friends.steve = new Object();

var friends = {
    bill: {
        firstName: 'Bill',
        lastName: 'Gates',
        number: '(098) 765-4321',
        address: ['Clemson', 'SC', '02987'],
    }
};

var friends = {
    steve: {
        firstName: 'Steve',
        lastName: 'Jobless',
        number: '(123) 456-7890',
        address: ['Delray Beach', 'FL', '33444'],
    }
};

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

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


#3

Try deleting :

And just group the var friends together into something like this:

var friends = {
bill: {
firstName: 'Bill',
lastName: 'Gates',
number: '(098) 765-4321',
address: ['Clemson', 'SC', '02987']
},
steve: {
firstName: 'Steve',
lastName: 'Jobless',
number: '(123) 456-7890',
address: ['Delray Beach', 'FL', '33444']
}
};

After that i think that you lack this in your search function inside the if body.

console.log(friends[key]);


#4

thanks for the suggestion enigma, unfortunately I'm getting an "unexpected identifier" error message.


#5

Used this code example to get by. Not really a fan of the output, but it is what it is...

Thanks to all that read!


#6

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