Search function didn't log "Bob Jones" to the console ( Lab 5/6 - finding that Special Someone)


#1

This is my code:

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

search("Jones");


But I keep on getting the following error:
Oops, try again.
It looks like your search function didn't log "Bob Jones" to the
console. Did you remember to call search after you defined it?

I am not able to make out what went wrong.


#2

For our purposes in this exercise,

var contactsLength = contacts.Length;

can be removed. It's where the error is, as well. Length should be length. But we may forego this step in favor of the direct query,

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

#3

Thank you so much :smile: