Not sure I understand the exercise?


#1


Ok, I've just finished the exercise and I think I kind of get it. My question is what is the point of this part?

var list = function (Object){
    for (var x in Object){
        console.log(x);}}

The code seems to work fine without this and I can't work out what purpose it has in the code?

var friends = new Object();

friends.steve = new Object();
friends.steve.firstName = "Steve"
friends.steve.lastName = "Jobs"
friends.steve.number = "999"
friends.steve.address = [9, "green", "way"]

friends.bill = new Object();
friends.bill.firstName = "Bill"
friends.bill.lastName = "Gates"
friends.bill.number = "222"
friends.bill.address = [2, "blue", "way"]

friends.Randy = new Object(); 
friends.Randy.firstName = "Randy"
friends.Randy.lastName = "Orton"
friends.Randy.number = "555"
friends.Randy.address = [50, "St. Louis", "Missouri"]


friends.John = new Object();
friends.John.firstName = "John"
friends.John.lastName = "Cena"
friends.John.number = "444"
friends.John.address = [40, "West Newbury", "Massachusetts"]

var list = function (Object){
    for (var x in Object){
        console.log(x);}}
     
var search = function (name){
    for (var x in friends) {
        if (friends[x].firstName === name){
            console.log(friends[x])
            return friends[x]
            }
        }
    }
var searchLastname = function (lastname){
    for (var y in friends) {
        if (friends[y].lastName === lastname){
            console.log(friends[y])
            return friends[y]
            }
        }
}

searchLastname("Cena")


#2

it lists all the properties in friends. Are you sure you want to use Object for this? it is a reserved keyword


#3

thanks for responding! Your right, I should change Object to something else. Still I don't understand why the list function is necessary? I think the code performs what I want it to without it. Why do I first need to list the properties?


#4

you don't, but it is the first function you build after constructing the objects, so it is a nice and easy function to see how to loop works, before moving on to the more complicated things like search.


#5

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