6. We Made a Friend! -Help please!


#1

add("Homer", "Simpson", "205-888-0000", "hsimpson@gmail.com");
console.log("Homer Simpson");
list();

I been stuck for the past two days and can't figure it out. I have looked at other examples to see if someone is experiencing the same issue I'm having but theirs seem to run fine. I can't spot the mistake, can someone help me please? I bet it's right in my face, lol,


#2

please copy paste your code to the forum so we can run it, the image is also very small


#3

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

/*Create a search function
then call it passing "Jones"*/
function search(lastName) {
var contactsLength = contacts.length;
for(var i = 0; i < contacts.length; i++)
if(contacts[i].lastName === lastName) {
    printPerson(contacts[i]);
}
}


function add(firstName, lastName, phoneNumber, email) {
    contacts[contacts.length] = {
        firstName: firstName,
        lastName: lastName,
        phoneNumber: phoneNumber,
        email: email
};
}
add("Homer", "Simpson", "205-888-0000", "hsimpson@gmail.com");
console.log("Homer Simpson");
list();

#4

the problem is in your list function maybe revisit the exercise so you can fix your list function?


#5

These are my results from my list() function:

Bob Jones
undefined
Mary Johnson
undefined
Bob Jones
undefined
Mary Johnson
undefined

Since it's printing out undefined, is that the reason why I keep getting the lastName error message?


#6

because its undefined? Why does your list function have an if statement? It should list all contacts, so an if statement doesn't make much sense

if you need more help, post an updated version of your code


#7

You know what, when I took out that if statement, it ran. I was probably half sleep when I threw that if statement in there. Thank you so much, you save the day!


#8