8 - Looking for clarity


#1


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


Ok so I fully understand all the components of this coding exercise and was able to get everything printed out the console just fine. However, I do not understand the goal of including the return friends[prop]; in this exercise. My printout looks like this:
steve
bill
{ firstName: 'Steve',
lastName: 'Jobs',
number: '415.555.7089',
address: [ 'One Apple Way', 'San Jose', 'CA', '94589' ] }
{"firstName":"Steve","lastName":"Jobs","number":"415.555.7089","address":["One Apple Way","San Jose","CA","94589"]}

However, the exercise states that there is a goal of:
"In this project, we'll combine our knowledge of objects and arrays to create a simple contact list. Then, using functions, we'll be able to log the entries in our contact list to the console, as well as search for a particular entry."

Wouldn't this mean that return friends[prop]; shouldn't have been included so my printout should simply be:
steve
bill
{ firstName: 'Steve',
lastName: 'Jobs',
number: '415.555.7089',
address: [ 'One Apple Way', 'San Jose', 'CA', '94589' ] }

I would also like to point out to anyone else going through this section for their first time to be wary of subsections 6 and 7. I noted that what was printed to the console in each of these subsections was not accurate to what coding was created. If anyone would like a further explanation feel free to message me.


var friends = {};
    friends.steve = {
        firstName: "Steve",
        lastName: "Jobs",
        number: "415.555.7089",
        address: ['One Apple Way', 'San Jose', 'CA', '94589']
        };
    friends.bill = {
        firstName: "Bill",
        lastName: "Gates",
        number: "589.555.2341",
        address: ['One Microsoft Way','Redmond','WA','98052']
        };

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

list(friends);

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

search("Steve");


#2

Returning friends[prop] would return a full person. For example, if when looping through, prop was Bill Gates, it'd return his full contact information. In what format? I'm not sure, but if you're curious you can try running it outside of codecademy.


#3

Thank you for a prompt reply!

So I used the following JavaScript online editor to quickly check out what another console may print: http://www.webtoolkitonline.com/javascript-tester.html. (You have to use the developer tools in the browser you are using to see the console; I'm using Google Chrome).

Here was what was logged when I keep return friends[prop]; in my code:

When I removed return friends[prop]; from my code, this is what was logged:

With the understanding that console.log and return both serve entirely different purposes, I'm assuming Codecademy and this JavaScript online editor are simply logging return friends[prop]; so that the user can see there is additional activity taking place 'behind the scenes'?


#4

Try your code in Chrome browser and you will see a little arrow (<-):

That's Chrome's way of telling you that it's displaying the return value of the function you called. So, the return value of that function hasn't been explicitly logged to the console, it's prefixed with the arrow to show that it's a return value printout for easier debugging.


#5

Thank you for confirming!


#6

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