Lesson 8 Victory - Code does not work and I don't understand what happens inside the functions


#1

What is wrong with my code?

Also could you please explain what (for... in) does? I am really struggling with this one. The best I could do is copy the answers. Even then I cannot get it to work.

I also don't understand what this line of code means

if (friends[key].firstName === name)

Also, why do we do both console.log(friends[key]); and return friends[key]?

var friends ={ 
    bill: {
        firstName: "Bill", 
        lastName: "Gates",
        number: "(209) 555-5555",
        address: ['One Microsoft Way', 'Redmond', 'WA', '98052']
    },
    
    steve: {
        firstName: "Steve", 
        lastName: "Jobs",
        number: "(408) 555-5555",
        address: ['One Hacker Drive', 'Cupertino', 'CA', '97054']
    } 
} 

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


#2

This implies there is a function with the identifier, name() which there is not so this will throw a fatal error which displays in the console, in red. The name of the search function is, search() so you should be writing,

search("Bill");

The code itself is correct but looks very much like a copy. This is suspicious. Does one really expect to learn how to solve problems when all we're doing is pasting in solutions? Hardly. Expect difficulty in the future if you cannot follow instructions, or formulate a plan and solve problems on one's own.

When iterating an array, we use,

for (var i = 0; i < array.length; i++) {

}

which will iterate over the array from first index (0) to last index (array.length - 1) in sequential order. This is why we sometimes refer to an array as a sequence. What defines a sequence is any list of numbers that are, a) in order from lowest to highest, or highest to lowest; and that, b) are the same difference or distance apart on the number line.

0, 1, 2, 3, ... is a sequence with difference 1 between each tern

0, 2, 4, 6, ... is a sequence with difference 2 between each term

Objects are not sequential and do not have a numeric index. They are associative arrays with key-value pairs that can be enumerated in any order. We call this an unordered data structure.

for..in

is an enumerator that seeks out the keys and their associated values, each in turn, but as stated in no particular order. It just goes through the entire object.

for (var key in object) {
    console.log(key+": "+object[key]);
}

In the above, friends is the object, key is the key name, which in this case will be either bill or steve. (Notice the lower case that matches the primary keys of the friends object?)

Both of the keys are associated with an object which has four properties. The key names are firstName, lastName, number and address. These names are fixed and common across all the primary properties.

The enumerator does not see them, only the keys bill and steve which it encounters during the enumeration process. Since we know that both of these have common properties (with unique values in each) we may address them directly, by name. Hence,

friends[key].firstName
         ^       ^
         |       |
     variable  known property

When key is bill, it will look like this,

friends['bill'].firstName      // Bill

When key is steve, it will look like this,

friends['steve'].firstName     // Steve

#3

Hi!

Thanks a lot for response and for explaining the sticking point.