Finding someone special


#1

Below is the line of code that is CORRECT, but I'm not satisfied. I get the list as the result and because it contains Bob Jones, it makes it correct. If the search is run correctly, it should just return Bob, shouldn't it?

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

search("Jones");


#2

It depends of how printPerson() function was created. For example:

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
}
// prints Bob Jones

If you want to print only firstname, just delete person.lastName:

function printPerson(person) {
    console.log(person.firstName);
} // prints Bob

#3

Thanks Denys - I didn't realize that it would log from the List function. I thought if you searched jones, it would only list bob's first name. But what you're saying makes sense. One other thing, it printed out Mary's name too, which leads me to believe that my search function isn't totally correct.

Thoughts around that issue?


#4

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