Contact List


#1

Hi ,
i have two questions for my doubt:

1 i want to know why : for(var firstName in friends)
i considered that only one way to expression :for(var i=0,i++;i<string.length)
why it works for(var firstName in friends) ?
2 use the var list = function(friends) two times ?
i could not understand this
thanks

friends.bill=new Object();
friends.bill.firstName="Bill";
friends.bill.lastName="Gate";
friends.bill.number="3453445";
friends.bill.address=["34","avenue","washing","340013"];

friends.steve=new Object();
friends.steve.firstName="Steve";
friends.steve.lastName="Job";
friends.steve.number="23322";
friends.steve.address=["57","avenue","China","pekin","433333"];
var list=function(friends){
for(var firstName in object)
console.log()
var myString = "Hey";
console.log();
};
var list = function(friends) {
for(var firstName in friends) {
console.log(firstName);
}
};


#2

Is there code missing?

Also, will you copy the link to the question?


#3

no,it passed the lesson ,as i saw the code of another ,
i donot understand the code .this is why i wrote here


#4

It looks incomplete and it's hard to tell the context of this code.

You should be able to go back to that lesson and paste the link here.


#5

https://www.codecademy.com/en/courses/javascript-beginner-en-3bmfN/0/6?curriculum_id=506324b3a7dffd00020bf661
this is the link .but the codes are not problem .
i just donot understand the code .
thank you for your help .


#6

This was looking weird because you never actually call the functions, so I was wondering how you were getting anything to output. I now see that it is part of the exercise.

Answer to Question 1

General for loop rule:


  • User "for in" loops for objects


    • These are situations where the amount of members in object are NOT KNOWN


  • Use "for" loops for arrays


    • These are situations where the amount of members in object are KNOWN

    • Array have access to the .length property to identity the amount of members in the array


for in works in this case because:


  • the friends object has 2 members: bill and steve

  • for in will iterate over all members in the object

for will not work in this case because:


  • there is no way to know how many members are in the objects without knowing ahead of time
    How will you know when to end the for loop?

Answer to Question 2

There is no need for two functions to be assigned to the variable list. The first assignment to list would:


  1. only print undefined two (2) times, because console.log() is not given a parameter.

  2. be overwritten by the second assignment to list

It is important to know that list is not being used by you. In order to use it you would have to call it similar to:

list(friends);

There is some test code in the background of the exercise that is running your code. This is why it is important to use the same object and function names (e.g., friends, friends.bill, friends.steve, list and etc) that they provide in the exercise.

Does this help?


#7

thank you for helps .


#8

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