6/6 What's wrong with my code?


#1



"Oops, try again. It looks like you didn't log "Alexander Waumans" to the console. Did you remember to call list() after adding your new contact?" This is the error I receive.


function add(firstName, lastName, email, phoneNumber) {
    contacts[contacts.length] = {
    firstName: firstName,
    lastName: lastName,
    email: email,
    phoneNumber: phoneNumber  
    };
};
add("Alexander", "Waumans", "alexander.waumans@example.com","123456789");
list();


#2

i ran your code, and the error i got is that your search function is missing (which you created in this exercise

if you need more help, post an updated version of your code


#3

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);
}

function list() {	
   var contactsLength = contacts.length; // i put var contactsLength here
   for (var i = 0; i < contactsLength; i++) {
      printPerson(contacts[i]);
   }
}

/*Create a search function
then call it passing "Jones"*/

function add(firstName, lastName, phoneNumber, email) {
   contacts[contacts.length] = {
      firstName: firstName,
      lastName: lastName,
      phoneNumber: phoneNumber,
      email: email
    }
}

add("Alexander", "Waumans", "123456789", "alexander.waumans@example.com");
list();

As you can see, you problem was in scope of var contactsLength . In your case this variable was global and always returns 2.
After I replaced var contactsLength = contacts.length; inside function list() you will always have an actual contacts length.


#4

Yes it works! Thank you!


#5

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