6. we made a friend


#1

I guess I am tired and can't see what my mistake(s) is (are); Can someone help ? I am getting a "SyntaxError: Invalid shorthand property initializer"

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 j = 0; j < contactsLength; j++) {
		if(lastName === contacts[j].lastName) {
		    printPerson(contacts[j]);
	    }
    }
};

search("Jones");

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

add("Abel", "Brown", "abel.brown@faremail.com", "567-876-876");
list();

#2

something not right in: add function
It should be like that:

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


#3

Thx. I replaced the = by the : but I know it says that
Oops, try again. It looks like there are still only two items in the contacts array.Bob Jones
ReferenceError: firstName is not defined


#4

this is my new code but it still doesn't work (Oops, try again. It looks like there are still only two items in the contacts array and Bob Jones
ReferenceError: firstName is not defined
:

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(lastName === contacts[i].lastName) {
printPerson(contacts[i]);
}
}
};

search("Jones");

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

};

add("Abel", "Brown", "abel.brown@faremail.com", "567-876-876");
list();

what should i do ?


#5

in add function: instead of: firsName, It should be: firstName


#6

A big big thank you !!


#7

Hi, why we need to use

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

instead of

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


#8

Yeah whoever created this lesson does a terrible job explaining how to do this. After a little research I found out how to do it and by the way I ran it and the code worked.

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 (lastName === contacts[0].lastName){
printPerson(contacts[i]);
}
}
}
console.log(search(prompt("What's the lasat name of the person you're looking for?")));

function add(firstName,lastName,email,phoneNumber)
{
//this will add new object to end of contacts
contacts[contacts.length] ={
firstName: firstName,
lastName: lastName,
email: email,
phoneNumber: phoneNumber //took out comma here
} ;
}
add("Alex","Davidson","adavidxxxx@gmail.com","(301) 888-8888");
list();


#9

I agree, they did a terrible job of explaining this.


#10

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