Let's say we're looking for someone in our address book with a specific last name.
We can do this with a technique for searching arrays called "linear search". With it, we use a loop to check through all of the items in the array one-by-one until we see the item that we want.
We can apply linear search to print out all of the people that have a particular last name.
Instructions
We'll be creating a function that can search for people with a specific last name and print those people out with the
printPersonfunction.- Create a function called
searchthat takes a parameter calledlastName. Leave thelistfunction alone. - Like with the last exercise, define a variable and store the number of items in the array in it. (Since every function has its own context, or scope, you can call this variable
contactsLength, too, if you like!) - Create a
forloop that runs through all of the items in the array. For this step, the code forsearchis identical to that oflist. - The twist comes here: in the body of the loop, rather than printing out every single item in the array, add an
ifstatement that checks to see if thelastNameproperty of the object is equal to thelastNameargument. Have the function runprintPersonon the person if and only if thelastNameproperty of the person matches thelastNameargument. - At the bottom of the file, call the
searchfunction, passing in "Jones" as the last name to search for.c
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 (var i = 0; i < contacts.length; i++) {
if (contacts[i].lastName === lastName) {
printPerson(contacts[i]);
}
}
};
console.log(contacts.length);
search("Jones");
No comments:
Post a Comment