Any idea why I'm getting "undefined"?


#1



https://www.codecademy.com/courses/building-an-address-book/0/5?curriculum_id=506324b3a7dffd00020bf661#


Getting "undefined" but don't understand why. Would you look over it and give me a hint what's wrong ?


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


#2

this line:

printPerson(lastName);

lastName is just a just a string containing the last name:

console.log(lastName)
printPerson(lastName);

your printPerson function expects a object. Not a string

You might want to change what you pass in as argument when you call your printPerson function inside your search function


#3

So you're implying to knock off those quotes what I put when calling a search function, aren't ya ? That way I'll be searching for not for a string but for an object. Am I getting this right ?


#4

no, your search function call is fine.

your search function is also fine. But then when you found someone, you need to print it, so you decide to call your printPerson function:

printPerson(lastName);

but look at the printPerson function:

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
}

doesn't like you should pass a string as argument when calling the function. You could pass the object?

Let me give one more hint:

if (contacts[i].lastName

you use contacts[i] here, what is contacts[i] here? give you use a .lastName, i would say it is an object. .property is used to access property's in an object.


#5

Mate but by correcting my search function from string to an object I actually cocked it up 'coz the value of lastName is stored as a string and not as an object that's why I think it can't work now.

Feel free to correct me. I always appreciate feedback of my work.

(Now I'm getting "Jones 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 (contacts[i].lastName == lastName) {

        printPerson(lastName);
        }
}

}
search(Jones);


#6

i wasn't referring to your search function call. See my second answer


#7

Looking back through my code, I notice that I had "===" in my "if" statement.
.... as I look a bit closer, there are some details in the "if" statement that need to be adjusted.....
Looks like we need to replace printPerson(lastName) with printPerson(contact[i]);

Happy codin' !

your code:

    if (contacts[i].lastName == lastName) {
        printPerson(lastName);
        }

What I had on my page:

    if (lastName === contacts[i].lastName) {
        printPerson(contacts[i]);
    };

#8

two equal sign is compare, three equal signs is also compare but data type has to match. Don't see why data type is relevant at this exercise.

You are right, but it doesn't really explain why...


#9

Okay guys ..... I tried all suggestions and tried many ways I could think of.

This is the right code that let me pass ... and preview says what it supposed to.

Apparently my original code was ok just needed this minor fix --> printPerson(contacts[i])
just trying to comprehend why it works with contacts[i] but not with lastName

Thank you folks !


#10

i tried to explain that, your printPerson expects an object, contacts[i] is, lastName is just an string


#11

Wonder what would have happened if I had typed console.log instead of printPerson. Would I had passed ? In other word is there a difference between those two ?


#12

maybe you could indeed use a console.log to log directly. Don't know the exercise allows it.