Building an Address Book
Listing Everybody

Address book programs usually have a screen that lists all of the contacts. Let's build that feature.

We could write out separate lines of code to display all of the people like in the last exercise, but that's tedious. Instead, we can use a for loop to do this automatically.


We'll be creating a function that lists all of the users.

  1. Create a function called list that does not take any parameters.
  2. At the start of the function, define a variable to store the number of items in the contacts array. Call it contactsLength.
  3. All of the items in an array are numbered, starting at 0. To cycle through all of the elements of the array, create a for loop that cycles from 0 up to one less than the number of items in the contacts array.
  4. Inside of the loop, add code to call printPerson, passing in the element of the array that the loop is currently at.
  5. At the very bottom of the file, call the list function. The list function should then loop through every member of the contacts array and print its information.
Stuck? Get a hint! Hint
Get live, fast support from Codecademy Advisors when you get stuck. Learn more.

If you need a review of how to use for loops, click here. And remember the array.length property for getting the array length!

The number of items in an array can be determined with array.length.

To cycle to one less than a number in a for loop, you can use the < operator in the second loop parameter.

for loops have a number that is incremented. You can get the array item that is at that number with array[number].