6/6 Address book, My add function isn't working and i have no idea why...i need help!


#1

Here is my code for the address book.

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 search = function(lastName) {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
if (lastName === lastName) {
printPerson(contacts[i])
} else {
console.log("Item not found")
}
};
};

THIS IS WHERE THE PROBLEM IS.
WHEN I RUN IT< IT SAYS THAT THE OBJECT WASN'T GIVEN A FIRST NAME

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

add("Joe","Ping","joeping@example.com","(650) 999-9999");

list()

I'M ASSUMING I DID SOMETHING WRONG HERE AND I'M NOT SEEING IT.
HELP PLEASE.

IF ANYMORE INFO IS NEEDED PLEASE JUST ASK!


#2

We need to assign the formal parameters to a new object literal:

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

That object is appended to the contacts array.


#3

Thank you so much. I can't believe i was that dense but thank you!


#4

Hello,
I wrote the code and am still coming up with syntax errors. Any help would be appreciated.
Thank You.

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"*/
var search = function(lastName){
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
if(lastName === contacts[i].lastName);
printPerson(contacts[i]);
}
};
search("Jones");
var add = function(firstName,lastName,email,phoneNumber){
contacts [contacts.length] = {
firstName: firstName;
lastName: lastName;
email: email;
phoneNumber: phoneNumber;
};
};
add("Bob","Not","bobnot@test.com","561-867-8738");
list();


#5

The issue is in the add() function. Object literal syntax requires commas between properties. This code has semi-colons.

As an aside, in future please create a new topic rather than inserting your problem into somebody else's topic. This is a resolved and soon to be closed thread. Thank you.


#6

Yes sir. Sorry for that. Thank You.


#7