#6 We made a friend error Please help - super frustrated


#1

Code Below - my error says"reference error: contact is undefined"
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 = new Array();
contacts[0] = bob;
contacts[1] = mary;
console.log(contacts[1].phoneNumber);

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]);
} else{
console.log("No contact found");
}
}
};
search("Jones");

var add = function(firstName, lastName, email, phoneNumber) {
var contacts = {
firstName: firstName,
lastName: lastName,
email: email,
phoneNumber: phoneNumber
};
contacts[contacts.length]= contact
};
add("Linda","Chuck","lindaChuck@gmail.com","(302) 565-4548");
list();


#2

This line is in conflict with the array object, contacts and actually wipes is out and replaces it. Try using just contact.


#3

new error: looks like you didn't add an person object to the end of your contacts array


#4

So you've still got the contacts array intact, and have created an object literal named contact in your search() function? It might be necessary to refresh the page to restore the variables.


#5

here is what I have now: printing phoneNumber, Bob Jones, no contact found error oops "It looks like you didn't add an person object to the end of your contacts array. "
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 = new Array();
contacts[0] = bob;
contacts[1] = mary;
console.log(contacts[1].phoneNumber);

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]);
} else{
console.log("No contact found");
}
}
};
search("Jones");

var add = function(firstName, lastName, email, phoneNumber) {
contacts = {
firstName: firstName,
lastName: lastName,
email: email,
phoneNumber: phoneNumber
};
contacts[contacts.length]= contacts
};
add("Linda","Chuck","lindaChuck@gmail.com","(302) 565-4548");
list();


#6

Should read,

var contact = {
    firstName: firstName,
    lastName: lastName,
    email: email,
    phoneNumber: phoneNumber
 };

And...

Set assignment to = contact


#7

None of the other posts for this problem worked for me even when others said the information they received worked but I took bits and pieces of correct information from each to make this and it worked. Strange. Anyway hopefully it'll work for whoever needs it.

Oh and I took out some comments and that line: search("Jones"); just to clean it up because I was confused and those lines weren't helping. Also, not sure where semi-colons are appropriate when declaring functions but I put them after every curly brace and it works with them there or without when concerning declaring functions.

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]);
};
};

function search(lastName) {
contactsLength = contacts.length;
for (i = 0; i < contactsLength; i++) {
if (lastName = contacts[i].lastName) {
printPerson(contacts[i]);
};
};
};

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

add("Dude", "Dudeson", "email.com", "0000000");
list();


#8

We need to use an object literal:

contacts[contacts.length] = {
    firstName: firstName,
    // ...
};

#9

Oh I see... Well this was the only way I could get it to work but then there has been some talk of refreshing the browser and then it works. So I guess back to the drawing board and making friends with the refresh button haha.


#10

You got it to work, meaning the new instance did get added to the contacts array. But, there is a whole raft of other properties in the object that we don't see if we don't look for them. When your code is run, log contacts and look at the newly added object.

That is why it is better to use an object literal (in adsence of a custom constructor).


#11

Thank you!. That response helped me out. I was stuck for a while until I read that.


#12

this has a lot of baggage, which is the point I raised above. It is not something to throw around. Control the content of your data structures, at all turns, or suffer the performance hit and Bob knows what else may be in store.


#13

Thank You :slightly_smiling: Nice work


#14

Thanks for this! I too have been banging my head against this for a while now; was killing confidence. :frowning:

mtf - presumably we can switch the 'var contact' directly for contacts[contacts.length] in 'add'? seems to work ok. perhaps this is what the instructions were suggesting afterall ...

thanks again. onwards and upwards eh.


#15

Correct. We don't really need to create a variable, just assign the new object directly.

If you are familiar with Array.push(), that is another approach:

    contacts.push({ ..., ..., ..., ... });

We can create the object literal right in the parameter of the push() function.


#16

Ok, great, thanks again. Not quite got to Array.push() yet, but will look forward to it :slightly_smiling: cheers


#17

mtf thanks for the info. I've been busy for a few weeks now so I'll need to go back and study the terminology because some of what you're saying is lost in translation for me at a beginner's level. I'm pretty sure I understand the idea of what you're saying though and you're spot on. I don't just want to pass the lesson but rather to get the most effective route. Thanks for pointing out the pitfalls of this way and giving a better alternative. I'll definitely be going back to this one to really grasp the idea and apply what you're talking about. Thanks!!


#20