7/8 What's wrong with my code here :(


#1

var friends = new Object();
friends.bill = {
firstName: "Bill",
lastName: "Jobs",
number: "+91-9819409487",
address: ["5D Malayagiri", "Anushakti Nagar", "Mumbai", 400094]
};
friends.steve = {
firstName: "Steve",
lastName: "Gates",
number: "+91-9987348289",
address: ["64 Sriniketan", "Anushakti Nagar", "Mumbai", 400094]
};

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

list(friends);

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

};

search("Bill");

Error I get is ... SyntaxError: missing ) after argument list


#2

I don't think this is the source of your error, but I notice there's an inconsistency when you are logging the properties of friends[key1] where you did not include the "." before "lastName".


#3

So, this is what works for me:

  • Replace the friends[key1] to friends[key] everywhere in the search function because it's not going to work if you set another name than the one that's [key1] as the parameter when calling the search function.

  • Don't log every parameter of the key separately, log the whole thing ( console.log(friend[key] ) just to be more efficient.

Here's my search function:

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

Actually I couldn't figure out how to do the conditions in the if statement of the search function so thank you.

I hope that this helped :smile:.