WDI Fundamentals

WDI Fundamentals Unit 8


Your Turn Your Turn

Arrays Exercise


Instructions

  1. Write the code to perform the actions listed below in the "JavaScript" panel in the JS Bin editor.
  2. If you don't see the JS Bin below, please refresh the page.
  3. Click "Run" to run the code in the "Console" panel.
  4. After clicking "Run", to check variable values, type the variable name in the "Console" panel on the right and hit enter/return.

JS Bin on jsbin.com

Let's get started!

  1. Create a contacts array. This array should contain the names "Matt Smith", "Sam Davis", and "Ashley Jones".

    Note: Be sure to hit the "Run" button and then check your work in the "Console" panel after each step. To check to see what the contacts array holds at any point, you can type contacts; into the console panel.

  2. Create a variable dad and assign it the value of the second element (item) in the array, "Sam Davis".

    Hint: remember arrays are zero-based so the first element in the array has an index of 0. To access the first element in the array, we would use the following syntax: contacts[0].

  3. Update the first element in the array. The new value should be "Mark Williams".

  4. Use the pop() method to remove the last element from the array ("Ashley Jones").

  5. Use the push() method to add "Michelle Johnson" to the end of the array.

  6. Create a variable numberOfContacts and assign it the value of the length of the contact array.

    Hint: you can use the length property to get the length of an array.

Once you're satisfied, feel free to move on to the next lesson.

Stuck? Check out the solutions for assistance.


Ready to move on? Here's the next lesson.