Some all around questions on contact list


#1

this is the code:

var friends = new Object();
friends.bill = new Object();
friends.steve = new Object();

var friends = {
bill:{
firstName: "Bill",
lastName: "Gates",
number: "(351) 913 234 234",
address: ["The road", "12123", "yeyeyeye"]
}
};
var friends = {
steve:{
firstName: "Steve",
lastName: "Wtver",
number: "(351) 921 564 233",
address: ["Spam", "Kappa", "123"]
}
};

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

The result was {}, and i passed, meaning it didn't return any information, also, when i was doing the ones before, when i logged "Bill" on the list function, the answer was {} and "Steve". From that point on i'm just assuming something in the code is wrong and i'm not understanding what..

Also, if someone could explain the search function to me (spech the key parts) i'd be very interesting in it.

Thanks


#2

@arccoder12354,
-1 you are re-defining the friends object instead of expanding the friends object
Do a console.log( friends );

-2 you are calling the search =function=
within the =function= definition itself.......
Place

search("Bill");
search("Steve");

outside of the search function definition.

-3 +++++ nyc object and the for-in loop explained ++++++++++++++++

T h e - B a s i c s

An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it's associated VALUE

var nyc = {
       fullName: "New York City",
       mayor: "Bill de Blasio",
       population: 8000000,
       boroughs: 5
      };
nyc -object- S p e c i f i e d

The nyc object has 4 properties seperated by a comma-,
- a fullName property with property-key fullName and it's associated string value of "New York City"
- a mayor property with property-key mayor and it's associated string value of "Bill de Blasio"
- a population property with property-key population and it's associated number value 8000000
- a boroughs property with property-key boroughs and it's associated number value 5

for - in - loop - e x p l a i n e d

With the for-in-loop you have a Method
which will iterate over all properties of a given object.
At each iteration it will assign the property-key as a string
to a variable name of your choice.
Thus for (var x in nyc)
will lead to 4 iteration's
iteration-1 var x = "fullName";
iteration-2 var x = "mayor";
iteration-3 var x = "population";
iteration-4 var x = "boroughs";
As they want you to display the property-key and NOT it's associated VALUE
you would use
console.log( x );

If they wanted you to display the associated VALUE of the propery-key you would use
console.log( nyc[x] );


#3

@arccoder12354,

+++ friends list and search and for-in explained.
Look here for an explanation
object
function
for-in-loop
list-function
search-function
http://www.codecademy.com/forum_questions/545275fd8c1ccc8e3e000a23#comment-5457b12c9c4e9d87cb00003f