List Every Body


#1

https://www.codecademy.com/courses/building-an-address-book/0/4?curriculum_id=506324b3a7dffd00020bf661#


Prints the same name to the console twice and says "Oops, try again. It looks like your list function didn't console.log "Mary Johnson". Did you remember to call list(); after creating it?"


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(){
    contactsLength=contacts.length;
    for(x=0;x < contactsLength + 1;x=x + 1){
   return printPerson(contacts[x]);
    }
    
    };
   list();


#2

Code looks fine, but why did you do "x<contactsLength+1"? It should just be "x<contactsLength". Also it's better to use var x instead of just x, and also x++ is cleaner looking instead of x= x+1. Finally, inside the for loop, you only need to call printPerson, not return printPerson. Code below has the changes I mentioned in case I am not being clear. Good luck!

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(){
    contactsLength=contacts.length;
    for( var x=0;x < contactsLength;x++){
   printPerson(contacts[x]);
    }
    
    };
   list();

#3

If the return statement is reached in a function
the function is exited !!!!!!


#4

I figured out the answer shortly after I posted the question. But thank you
for the help I appreciate it. Also thanks for the tips on how to make my
code neater.


#5

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