# 8 Final Thoughts: Why did mine satisfy the previous project?


#1


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


I don't feel like mine was working the way it should, but it was "correct". Any insight what I did right, and what went wrong?


var friends = {
    bill: {
        firstName: 'Bill',
        lastName: 'Ding',
        number: '1-(800)-POTATOS',
        address: ['Four Euer Ave.','Franklin','DE','80085']
    },
    steve: {
        firstName: 'Steve',
        lastName: 'Vins',
        number: '1-(888)-SLP-CHOP',
        address: ['2 Much-Rocky Rd.','Franklin','DE','80085']  
    }
};

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

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


#2

The list() function iterates over the primary keys of an object. Using a specific variable name is misleading. The firstName properties are inside the objects associated with each key. Recommend use a generic variable that describes what is being iterated:

for (var key in friends)

Going one step further in the direction of a general utility function, naming the object that is passed in the parameter restricts the use of the utility to just the one object. A generic variable will permit passing in any object and listing the primary keys. That's called portable and reusable.

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

Again, choice of variable name is at issue here. The parameter in this case that would best describe the expected inputs is, name.

var search = function (name) {

};

steve is not a very meaningful variable name, either. Since the function is dedicated to the friends object, a singular form of the word would fit right in...

for (var friend in friends) {
    if (friends[friend].firstName === name) {

    }
}

The search function should not have an else branch on the if statement.

The above line is inside the function body and will cause problems if the function is actually run. It should use a proper name in quotes as an argument, and it should not be inside the function, but in global scope.

search("Steve");

#3

I thought the generic names were to be replaced with the names of the search code; so I was confused.


#4

You're not alone. It takes some working with variables and functions to begin to get the picture. The objective when choosing variable names is self-documentation. Names that describe what they are referring to (value, object, function) add meaning to the script components when reading the code. Names that describe purpose also fall into this group.

As arbitrary as it is what anything is called, it turns out not so arbitrary, after all. Notice the names used for the two functions in this program?

list()

search()

We know they are functions because they include parenthesis in their definition. Just as [] is an emty array, and {} is an empty object, (){} is an empty function. That aside, we get an idea of what they do. One lists, the other searches. The names are very generic, perhaps too generic, in a sense, though at the current level of learning they do just fine.

list_keys()

is more descriptive of what the function actually does.

search_firstname()

is also more descriptive of what the function actually does.

You will deal with variable names for your whole career so there's no point in me bleating on about how important meaning and semantic nomenclature is. But it does bear mention now and then.


#5

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