6> We Made A Friend!


#1


Error Message:
Oops, try again. Did you create a function called add?
ReferenceError: add is not defined

To be honest I rushed the Add function because I keep doing this one exercise with no success


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

search("Jones");

add('here', 'ye', '666-6666', 'satan@hell.org');
list ();


#2

You should make add() function, I think it's against the rules to give you the solution, and even if it's not it's pointless if you just copy something simple as add() function.

You just need a function that will take some data and make new person object which will then be inserted inside contacts array. It's simple really. Try it and if you fail, come back with your try code. :slight_smile:


#3

that...not copy-pasted


#4

I have a new error, it said "Oops, try again. It looks like there are still only two items in the contacts array. " Can you maybe point me in the right direction? Here's the new try code.

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

search("Jones");
I have a new error, it says "Oops, try again. It looks like there are still only two items in the contacts array. "

add = function (firstName, lastName, phoneNumber, email){
this.firstName = firstName;
this.lastName = lastName;
this.email = phoneNumber;
this.phoneNumber = email;
}

add('here', 'ye', '666-6666', 'satan@hell.org');

list ();


#5

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