Explanation Needed


#1

I was able to work through the contact list and use other form topics as a guide but I felt like I didn't really understand what I was doing and why I was doing it. Specifically after creating the contacts, when I got more into exercises 6 and 7. Can someone please just break this down for me as to what we are wring and why. I would greatly appreciate it.


#2

We start this project by creating a contacts object,, friends then inserting a number of empty objects:

var friends = {
    bill: {},
    steve: {},
    eric: {}
};

Next fill in the property data of each friends key.

firstName: "Bill",
lastName: "Gates",
number: "(317) 374-8570"

Each key consists of three properties, firstName, lastName, number.

Lesson 5 has us 'toss in an array' which has 4 elements:

 address: [ '123 Any Street', 'Any City', 'Any State', 'postal code' ]

The next part of the project is to list all the primary keys in the friends object. This will use a for..in statement that iterates through the keys. The function is to take a single parameter, an object.

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

The call to this function will include the friends object as the argument:

list(friends);

Exercise 7 pushes the envelope of our accumulated knowledge with a challenge to write a search function. The function will take one parameter, name and will both log the matched object, and return it.

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

The last instruction is to call the search function on Bill...

search("Bill");

Because we are both logging and returning an object, expect extraneous output to the display. It can be ignored, as can the output generated when the lesson checker runs your code to test it. As long as you see what is expected from your code, everything is good.

The last challenge is optional, and has no lesson checker (SCT). You can do whatever you wish and experiment all you want.


#3

I really appreciate both question and answer, was struggling myself until i read this. Much appreciated.

Cheers both.


#4

hello mtf

Your explanation is seriously too good and did solve the issues I had in 6/8.

I have copied your code and the result I get is bill & steve twice! [Why so?]

even if i dont call the function "list" with single parameter as "friends", in the output window yet I get bill & steve but then it doesn't happens twice.

I have given snapshot for both the scenarios.

Your help is indeed needed, thanks....



#5

As mentioned earlier, the lesson checker runs our code to test it, so the list() function is run twice, once by us, once by the SCT Submission Correctness Test). We will see some extraneous output in this project.


#6

thank u so much for your help buddy!!