Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
JavaScript
var friends= {
    steve: {
    firstName: "steve",
    lastName: "Sonwane",
    number: "9867707997",
    address: ['bld.no 234 N.C.H colony']
    },
    sunil: {
    firstName: "Sunil",
    lastName: "Sonwane",
    number: "9768912320",
    address: ['bld.no 234 t.C.H colony']
    },
};
  
    
var list = function(friends){
    for(var key in friends){
        console.log(key);
        }
        };
    
var search= function(name) { 
    for (var key in friends) {
        if (friends[key].firstName === name) { 
            console.log(friends[key]);
            return friends[key];
        } else {
            console.log("couldn't find them");
        }
    }
};

search("steve")


What I have tried:

tried changing object structure to something like friends.steve = {then key's ad values}

but did'nt work ! maybe a syntax error !
Posted
Updated 30-Jun-16 9:40am
Comments
Mehdi Gholam 30-Jun-16 14:36pm    
How is it not working?
Andriken 30-Jun-16 14:46pm    
i did the same as here he claims and proven at the same place where im doing this ! but my did'nt work ... see here http://stackoverflow.com/questions/31032001/codeacademy-contact-list-building-7-8-not-returning-contact-info/31032212
ZurdoDev 30-Jun-16 14:37pm    
Who knows? You haven't told us what the error is or what is happening.
Andriken 30-Jun-16 14:41pm    
i dont know how much more clarification you guys need even same as you i think it's right code but the codeacademy say's it's not, throwing an error saying "syntax error !"
ZurdoDev 30-Jun-16 14:46pm    
Why do we need more clarification? Because you haven't even asked a question. I am close to finishing my mind-reading courses but I haven't completed just yet so pardon me when someone dumps some code and says "maybe a syntax error" and I don't know what they are asking. If there is a syntax error, does codeacademy tell you what line?

If not, google for javascript lint online and you'll get some where they can check the syntax for you.

1 solution

Look very carefully at the end of the data structure. Looks like you have a comma more than you need.

The address strings also look a little odd. What's with the square brackets? Try changing them to, for example, "bld.no 234 N.C.H colony".

I take it that friends isn't meant to be a JSON structure. If it is then the opening and closing curly brackets should be square [].

var friends= {
steve: {
firstName: "steve",
lastName: "Sonwane",
number: "9867707997",
address: ['bld.no 234 N.C.H colony']
},
sunil: {
firstName: "Sunil",
lastName: "Sonwane",
number: "9768912320",
address: ['bld.no 234 t.C.H colony']
}, <----. Stray comma?
};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900