7. Search for a Friend -- not returning Bill


#1

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

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

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "(555) 555-5555",
address: ["here is words", "to put in a list"]
}
};

var friends = {
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(555) 555 -5555",
address: ["here is more words", "this is confusing"]
}
};

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

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

Bill's information is not being returned I am so confused.


#2

i changed my list variable to
var list= function(friends) {
for (var key in friends)
console.log(key);
};


#3

I believe you now have it right? :slight_smile:


#4

This is the code I got to work:

var friends = {};
friends.bill = {
    firstName: "Bill",
    lastName: "Bryson",
    number: "02089945685",
    address: ["321 Fake Street", "London", "England","SE1 1AL"]
}
friends.steve = {
    firstName: "Steve",
    lastName: "Dangerous",
    number: "02079985685",
    address: ["The Backett Building Penthouse", "London", "England","N1 1XX"]
};

var list = function(targetobject){
    for (var x in targetobject){
        console.log(x);
    };
};

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


search("Bill");

console.log("endofcode");

#5

Ok, my issue was different. I've now changed my above post to the correct code.


#6

You are always write (VAR friends)-its mean create new object. Delete "var", cause you are create object in first line;


#7

I took the var out before steve and it still won't return bill :,(
thank you for pointing that out though.
Does anyone else have any suggestions? I feel like I get the material but I'm stuck and I can't move forward.


#8

Take away the code and write again. Sometimes it helps .


#9

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