Listing Everybody---someone plz explain me, how loop works, need help!


#1

var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};
var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}

var contactsLength = (bob, mary);

var list = function() {
for(var i=0; i <= contactsLength; i++) {
console.log(printPerson(contacts[i]));
}
};
list();


#2

Can you post a link to a lesson?


#3

We wanna count how much objects we have in contacts.

var contactsLength = contacts.length; // (2)

Now your loop should work


#4

sure. here is the link
https://www.codecademy.com/courses/building-an-address-book/0/4?curriculum_id=506324b3a7dffd00020bf661


#5

got it...thanks, list was not function the way i used, so i change format of function...and its working, thanks....


#6

Final Mark up

var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
}

var list = function(){
//calculating the lenght of the contacts array
  var contactsLength = contacts.length;
// replacing contacts.length with contactsLength variable in the below for loop
  for(i=0;i<contactsLength;i++){
//accessing contacts array
    printPerson(contacts[i]);  
  }
};
// calling list function
list();

#7

hey thanks and appreciated, i already finished that...Thank u..


#8

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