Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Stuck again. Help Please

Its keeps telling that something is wrong with my syntax.

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, dale];

var contactsLength = contacts.length;

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,email,phoneNumber)
{contacts[contacts.length] =
{
firstName: firstName,
lastName: lastName,
email: email,
phoneNumber: phoneNumber
};
contacts[contacts.length]=object;
};

add("Dale","Vasquez","DaleVasquez@example.com","866-329-3468");

list()

Can you post the error? A good way to check these is to run the files from your computer and not in the workspace. This way you can take advantage of the console errors and debugging available in the browser. I'm not sure if you are still allowed to download the files, but you can at least copy and paste the html and js into new files on your computer.

One thing I notice is you need a comma after your phoneNumber key-value pair in bob.

3 Answers

Hey Gary Christie,

There are a few errors that need addressed. The first one is that you are missing a comma after the "phoneNumber" key for the "bob" object.

The second one is that you are adding the "dale" object to the contacts list before the dale object is initialized. This list should be truncated to only those objects that are defined, which would be "bob" and "mary".

The third one is that inside of your "add" function, you are trying to set the new contact object to be equal to the keyword "object" which won't do anything. This line should be deleted as it does nothing towards your program.

Here is the updated JavaScript:

//Added comma to end of phoneNumber key/value pair
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"
};

//Deleted unknown dale object from contacts variable
var contacts = [bob, mary];

var contactsLength = contacts.length;

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,email,phoneNumber){
  contacts[contacts.length] = {
firstName: firstName,
lastName: lastName,
email: email,
phoneNumber: phoneNumber
};
//Deleted variable declaration because it overwrote object declaration with object keyword
};

add("Dale","Vasquez","DaleVasquez@example.com","866-329-3468");

list();

Thank you for your help

Did that work for ya, Gary?

Yes sir. I appreciate it

I'm glad to hear it! If you want to go ahead and mark my answer as Best Answer, we'll go ahead and close this question out.