WDI Fundamentals Unit 11
Now that we have a way to store keys and values, we can add some complexity to our contact book. Are you ready?
Again, our goal is to create a contact book that allows us to store contact info (including first name, last name, phone, and email address), add a new contact, list contacts, and find contacts by first or last name.
Before reading the instructions below, take a few minutes to think through the problem and try your hand at writing out pseudocode. Although the instructions will guide you through the steps for building the program, it's helpful to practice thinking about your own approach. Programmatic thinking becomes easier with practice!
1) Write the code to perform the actions listed below using JS Bin editor. If you don't see the JS Bin, please refresh the page. 2) Click "Run" to run the code in the "Console" panel. 3) Call each function from the "Console" panel. Try using different parameters to make sure they're working properly.
contactsarray should contain three objects, one for each contact stored in our book. Here's the contact information for each person:
addContact, that accepts one parameter,
newContact. This function should add the parameter to the end of the
contactsarray and return the
contactsarray. When you're done, try calling the function and passing in an object as a parameter to make sure it's working.
listContactsfunction to list our contacts. This function should loop through the
contactsarray and log the first and last name for each contact to the console, i.e.
contactsarray and return the contact info for any person who has a matching first or last name. Create a
searchfunction, which will take one parameter,
name. In the
searchfunction, use a loop to iterate through the
contactsarray and return the current contact if his or her first or last name matches the
You've done it! We now have a working contact book that allows us to store contacts, add contacts, list contacts, and find a contact by first or last name. Congratulations!
Stuck? Check out the solutions to see what you can do.
Feeling confident? Test your understanding of iteration with this next quiz.