Finding that special someone


#1


5.Finding that Special Someone


"Oops, try again. You have the following error in your code: TypeError: Cannot read property 'lastName' of 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 = [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 (lastName === contacts[i].lastName) {
            printPerson(contacts[i]);
        }
        
        else {
            console.log("Who???")
        }
    };
}

search('Jones');


#2

Problem is here:

for(var i = 0; i <= contactsLength; i++) {

So, in the last iteration i will be equal to contactsLength. We start indexing elements in arrays from 0. So if we have array ["a", "b", "c"] element at index 3 (length of this array) does not exist, right?

You have to change condition to:

i < contactsLength

Logic behind this else:

else {
    console.log("Who???")
}

is faulty. Can you solve this problem without our help?


#3

Thanks! It was the console.log statement not having a semicolon, if I am correct? (I added one and it worked.)


#4

I meant something else, but maybe this was how you wanted to do it.

Let's add few more people to our contacts:

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 john = {
	firstName: "John",
	lastName: "Smith",
	phoneNumber: "(650) 999-9999",
	email: "smith@example.com"
}

var anne = {
	firstName: "Anne",
	lastName: "Jones",
	phoneNumber: "(650) 666-6666",
	email: "anne.jones@example.com"
}

var contacts = [bob, mary, john, anne];

Now, your code returns:

Bob Jones
Who???
Who???
Anne Jones

And I guess that it would be more practical to print Who??? if a person with this last name wasn't found.

To do it we can simply add a flag - boolean value to indicate if contact was found:

var search = function(lastName) {
    var contactsLength = contacts.length;
    var contactFound = false;
    for(var i = 0; i < contactsLength; i++) {
        if (lastName === contacts[i].lastName) {
            printPerson(contacts[i]);
            contactFound = true;
        }
    }
    if(!contactFound) {
    	console.log("Who???")
    }
}

And now the result is:

Bob Jones
Anne Jones

#5

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