Search for a friend... HELPPPP


#1

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

My code doesn't have any errors but it prints out wrong data!!
What it prints:
bill
jon
steve
Bill Found!
{"firstName":"Bill","lastName":"Gates","number":"45465","address":["somewhere","far"]}

var friends={
    bill:{
        firstName: "Bill",
        lastName: "Gates",
        number: "45465",
        address: ['somewhere','far']
        },
    jon:{
        firstName: "Jon",
        lastName: "Smith",
        number: "45655",
        address: ['somewhere','far']
        },
    steve:{
        firstName: "Steve",
        lastName: "Jobs",
        number: "454354",
        address: ['somewhere','far']
        }
};

var list=function(param){
    for(var man in param){
    console.log(man);
    }
}

list(friends);

var search=function(name){
    for(var ky in friends){
        if(friends[ky].firstName===name){
            console.log(name+" Found!");
        }
            return friends[ky];
    }
};
    search("Steve");


#2

return should be inside your if clause. Otherwise the function will finish in the first run, no matter if it found your friend.


#3

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