5. Finding that Special Someone


#1

My code looks like this:

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){
for (j=0;j<=contacts.length;j++){
if (contacts[j].lastName === lastName){
printPerson(contacts[j]);
}
}
}
search('Jones');

THE ERR MSG: Oops, try again. You have the following error in your code: TypeError: Cannot read property 'lastName' of undefined


#2

Can't figure out why this is an error.


#3

Here, the additional "=":

That will cause the for loop to work contrary to what you intend. See below:

    for(j = 0; j < contacts.length; j++){

#4

Yeah man, the contacts.length is 2, but j starts with 0, I must be dumb...


#5

Naaa... You're not dumb.

Have fun coding! :slight_smile:


#6

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