Adding a new object to the array (Debug needed)


#1


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) {
    for (var i = 0; i< contacts.length; i++) {
        if (contacts[i].lastName === lastName){
            console.log(contacts[i].firstName + " " +contacts[i].lastName);
        }
    }
}
search("Ink");

function add(firstName, lastName, email, phoneNumber) {
    this.firstName: firstName;
    this.lastName: lastName;
    this.phoneNumber: phoneNumber;
    this.email: email;
}
contacts[contacts.length] = add;
add("Phantom", "Ink", "email@example.com", "(909)890-9374");


#2

So close with this one.

You need to add this code into your add function. Put it above the first this and replace the add keyword with a curly bracket.


#3

I added that in, and am now getting this error:
SyntaxError: Unexpected token '.'. Expected a ':' following the property name 'this'.


#4

Can I see the updated code?


#5

Actually there is no need to use this here.


#6

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) {
for (var i = 0; i< contacts.length; i++) {
if (contacts[i].lastName === lastName){
console.log(contacts[i].firstName + " " +contacts[i].lastName);
}
}
}
search("Ink");

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

add("Phantom", "Ink", "example@example.com", "(909)890-9374");


#7

Need to use commas instead of semi-colons.


#8

Now I'm getting:
SyntaxError: Unexpected token ';'. Expected a ':' following the property name 'firstName'.
I've fixed the ":" to a comma.


#9

Specifically on:
firstName, firstName;
and the closing curly bracket


#10

That is not a semi-colon, this is a semi-colon ;.


#11

Ohhhh. Oops! I see what you meant now!
my bad.


#12

No worries, is it all good now?


#13

Yeah! That's what fixed it!


#14

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.