What is wrong with this code for exercise "We Made a Friend!


#1


Replace this line with your code.


#2

this code is getting an error "Oops, try again. It looks like you didn't add an person object to the end of your contacts array."

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

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

contacts[contacts.length] = add("Martha", "King", "555-444-3333", "martha@gmail.com"); 

list();

#3

the add function will add the contact to the array, then when calling add, you don't need to store the result of function call (which returns none) in array


#4

Thank you, i just called the add function, but did not assign it to the contacts array. Then it worked. Thanks so much. Really appreciate it!


#5

by default functions return None, so by assigning to contacts array on function call stores None in contacts array, something you don't want

Good you solved it :slight_smile:


#6