7. Search for a friend. Weird output!?


#1

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

THE PROBLEM
I am talking about the output of this code which you can find below.

The first part makes sence to me, it is what I expect. I ask the program to list every property of the object friends (by calling the function at the end). And it gives me Bill and steve.

But then I ask for the firstName of any object within friends that has the name Bill. And it gives me two more Steve's just for free? And then what about Bill not being defined. Isn't he an object? Please help!

THE CODE

var friends = {
    Bill: {
        firstName: "Bill",
        lastName: "the fat one",
        number: "555-123 456",
        address: ['Street, postalcode, city']
    },
    steve: {
        firstName: "Steve",
        lastName: "the small one",
        number: "555-123 456",
        address: ['Street, postalcode, city']
    }
};

var list = function(category) {
    for (var prop in category) {
        console.log(prop);
    }
};

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

list(friends);
console.log(" ");
search(Bill);

THE OUTPUT

Bill
steve

Steve
Steve
Bill
ReferenceError: Bill is not defined.

THE THANKS FOR HELPING PART
Thanks for helping!


#2

Remember to write within quotes. "Bill".


#3

Changed the part below. But now the output makes even less sense to me. Although I did
learn to use quotes correctly now, so thanks for that.

NEW CODE

list("friends");
console.log(" ");
search("Bill");

NEW OUTPUT

0
1
2
3
4
5
6
 
Bill
Steve
Steve
Bill
{"firstName":"Bill","lastName":"the fat one","number":"555-123 456","address":["Street, postalcode, city"]}

EDIT: Makes sense why Bill was not defined. It was looking for a function or var called Bill. Not the object.


#4

This property name should be lowercase, bill. For best results use the data provided in the instructions. When we stray from the instructions we run the risk of our work being rejected, even though it may work properly.


#5

Also makes sense. I changed the objects to be lowercase (bill and steve) and their first names are Bill (with capital) and steve (without) as the instructions say. This still gives me the numbers in the output (as previous). It just gets rid of the first Bill and Steve after the blank-line.

Any idea? Thanks so far!


#6

I added a line after I call the search function as follows:

console.log("=====THE END=====");

This now shows me that I do indeed just print out the name steve. Then comes the end part. And then after if just prints out some more stuff. Learned that from here, helped a lot!

Still doesn't fix the numbers sadly


#7

AAAHHH

It finally clicked.

I should not go list("friends"); because in this case I AM looking for an object called friends so the correct thing is list(friends);. Still not getting all steves information on the return part, but that can't be that difficult can it?

Thanks again!


#8

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