Stuck on We Made a Friend!


#1

Hi all,

I breezed through this lesson then of course got stuck at the end. The error I'm getting is "SyntaxError: Unexpected identifier." Please let me know what I'm doing wrong here. I'm thinking I'm adding them to the contacts array incorrectly but I've tried a few different ways at this point and I'm stumped.

Thanks!

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

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

add("Erica", "Correa", "(xxx) xxx-xxxx", "xxx@xxx.com");

list();


#2

This is close, but no cigar...

contacts[contacts.length] = this firstName;
    this firstName = firstName;
    this lastName = lastName;
    this phoneNumber = phoneNumber;
    this email = email

The code is similar to a constructor function, which is ill suited to literal construction which uses object literal syntax, not function statement syntax.

    contacts[contacts.length] = {
        firstName: firstName,
        lastName: lastName,
        phoneNumber: phoneNumber,
        email: email
    };

#3

Thank you so much! That definitely makes more sense!