Anyone felt that contact list was a very bad learning experience?


#1

Contact List Nr 7: Search for a friend.

I just cant find whats wrong with this code. Codecademy tells me "Oops, try again. It looks like your search function doesn't return contact information for Bill.". In the console it just shows me the content of both objects. I already looked up the hint and the example code from the first chapter, but I just cant find any errors.

Also while I am a big fan of codecademy, I feel like this chapter and especially the search and list functions where a bad learing experience, where the lessons did not explain the syntax nearly enough to really understand how to go through the keys of an object. For the first time i felt way overboard and badly prepared and just kept looking at the example code in the first chapter over and over again.


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "(206) 555-5555",
        address: ["One Microsoft Way",'Redmond','WA','98052']
    }
    
};


var friends = {
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "089 123456",
        address: ["Hauptbahnhof",'81000','München','Bei Hausmeister einwerfen']
    }
    
};


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

search ("Bill");


#2

sorry to hear that you find this to be a bad learning experience. Lets hope i can add some clarification. The error caused by the lesson has nothing to do with the search function, it is fine

But rather, with how you create the object:

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "(206) 555-5555",
        address: ["One Microsoft Way",'Redmond','WA','98052']
    }
    
};


var friends = {
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "089 123456",
        address: ["Hauptbahnhof",'81000','München','Bei Hausmeister einwerfen']
    }
    
};

what is happening? you create an objects friends, which contain object bill. Then you overwrite object friends, and this time it has an object steve.

Both bill and steve should be part of the friends object:

friends = {
   bill: {
      /* property's */
   },
   steve: {
      /* property's */
   }
}

#3

Dear stetim, thank you for clearing this up. I think I got confused because of the two or even three ways you create objects and fill them with properties.

However I wonder, because I created the "list" function in the exercise before which should list the two names of the friends in the console, which did work. According to your explanation it should not have been able to find both?

On another note, I think this exercise might not be that bad, but it just was way to early and should only be available later on after objects I or even objects II.


#4

yes, this is confusing. You might want to write them down. And here you have a friends object which contains two more objects (bill and steve)

it shouldn't have been able to find both, did you see both in the console? IT might have validated correctly, because your list() function was correct, i would have to see how the exercise gets validated.

True, that might have been better. The javascript course is going to get a upgrade, so then this problem no longer exist :slight_smile:


#5

Yes it's useless and not working for no reasons :frowning:


#6

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