We Made a Friend! - my new contact has no firstName property?


#1

Hi,

can anyone see the problem with my code? This is the message I get: "Oops, try again. It looks like your new contact doesn't have a firstName property."

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

/*Create a search function
then call it passing "Jones"*/
function search(lastName) {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
if (contacts[i].lastName = lastName) {
printPerson(contacts[i]);
} else {
console.log("No such person is found.");
}
}
}

function add(firstName, lastName, email, phoneNumber) {
new Object(this.firstName, this.lastName, this.email, this.phoneNumber); // ˘V(.%)V˘
//contacts.push(new Object); ==> SYNTAX ERROR!!!! This didn't work.
contacts[contacts.length] = new Object; // I'm really not sure about this.
}

add("Cassey", "Kfjios", "casseykfjios@example.com", "01020304...");


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

add("Conor", "Example", "conorexample@gmail.com", "123456789");
list();

#3

Thank you, it worked.


#4

I did the adding code differently:

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

And that worked.


#5

were you able to call the function in the same way?


#6

I had the add() create the new contact, then return that contact. So I called contacts.push(add("first", "last", "email", "phone"); and it worked.