7. Search for a friend


#1



Need Help!

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


My code outputs "bill" and "steve" and i want it to only output "Steve"


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "001",
        address: ["Microsoftkatu", "2A12", "Windows"]
    },
    steve: {
        firstName: "Steve",
        lastName: "Jobbs",
        number: "002",
        address: ["Omenatie", "49", "Apple"]
    }
};

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];
        }
    }
}

list(friends);
search(friends);


#2

Two problems:

  1. Your code outputs both steve and bill because you are calling list function.
  2. Statement search(friends) won't work as expected because search function expects a string name as an argument, and what you are passing it (friends) is an object.

#3

I chanced it and now it doesn't output anything? :confused:

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "001",
        address: ["Microsoftkatu", "2A12", "Windows"]
    },
    steve: {
        firstName: "Steve",
        lastName: "Jobbs",
        number: "002",
        address: ["Omenatie", "49", "Apple"]
    }
};

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];
        }    
    }
}

search("Steve");

#4

The objects bill and steve have a property firstName. But you are doing this:

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

Problem spotted? :wink:


#5

Sorry, I still don't get it :slight_smile:. The hint said to do it like that...


#6

OH
Now i do :stuck_out_tongue_winking_eye:. Oops :stuck_out_tongue:


#7

Thx for the help :wink:. Really needed it