6.We Made a Friend!


#1

Getting a "pat is not defined" error

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

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

search("Jones")

var add = function(firstName, lastName, email, phoneNumber){
var newEntry = {
firstName: firstName,
lastName: lastName,
email: email,
phoneNumber: phoneNumber
}
contacts.push(newEntry)
}

add ("Pat", "Tillman", "pat.tillman@example.com", "(123) 345- 5435")


#2

This is not where to add the new contact. Append it to the array inside the add() function.

The code you have in that function is valid and correct. It appends the anonymous object to the array meaning it has no outside reference object. contacts[2] is the only reference to that element.

This is in contrast to the earlier objects we created. They are not in the array, but referenced only. One of the hidden lessons here is the advantage that comes from building objects directly in the array. They cannot be accidentally modified of removed.

If we change bob or mary directly, it impacts on the array references. If we remove one or both, they also disappear from the array, almost. The references will now raise a type error as they will now be undefined.


#3

I don't think I understood what you said, here's my code. From what you said I thought that you have to add the contacts inside the add function but it doesn't seem to be working.

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"
};

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

search("Jones")

var add = function(firstName, lastName, email, phoneNumber){
var newEntry = {
firstName: firstName,
lastName: lastName,
email: email,
phoneNumber: phoneNumber
}
contacts.push(newEntry)
var contacts = [bob, mary, add.list]
}

add ("Pat", "Tillman", "pat.tillman@example.com", "(123) 345- 5435")
list()


#4

bob and mary are object literals defined in global scope. We initialize the contacts array to include references to them. There are not copied into the array. That is why we may refer to bob or contacts[0], They are the same object.

The array should ony contain those literal references and nothing else. All new contacts will be written directly to the array, only. We do not add any more identifiers to the array. Anonymous objects don't have names.


#5

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