Arrays
Unfortunately, strings and numbers are not enough for most programming purposes. What is needed are collections of data that we can use efficiently, Arrays.
Arrays are great for:
Storing data
Enumerating data, i.e. using an index to find them.
Quickly reordering data
Items in an array are stored in sequential order, and indexed starting at 0
and ending at length - 1
.
Pair Exercise: String Manipulation
With your pair, have one person create a variable that equals a comma delimited string with at least four of your favorite foods.
Have the second person turn that string into an array, then the first person should retrieve the third favorite food from the array.
Iterating Over Arrays
Often we want to write code that interacts with everything in an array. Let's say we have a list of numbers representing sub-totals on a receipt.
For a moment, let's assume we have four items on our receipt. We could "hard-code" a program to manually add up all of the items:
We say the above example is "hard-coded" because it's brittle. The code assumes there's exactly four items in the array. The code would cause an error if there were less than four items. The code would fail to add anything beyond the first four items in the list. Instead of hard-coding our program we can use a for loop to iterate over the array and calculate a total no matter how many things are in the array!
Iterating over arrays is very common.
Pair Exercise: Finding Averages
With your pair, use a for loop to iterate over an array. Calculate the average value of all numbers in the array. Consider how your code would operate on empty arrays, arrays with one value, arrays with two values, and arrays of any length. Test your code on each of the following arrays:
Array Methods
Arrays come with special methods attached to themselves that allow you to perform common operations.
Adding and Removing Single Items
.push('element')
- add an element to the end of an array.pop()
- remove and return the last element in an array.shift()
- remove an element off the front of the array.unshift(3)
- add an element to the beginning of an array
Reorder an array:
.sort()
- sort the elements in an array.reverse()
- reverse the array
Other useful methods
.concat([1, 2])
- concatenate two arrays together.slice(1, 3)
- return a copy of a portion of an array.splice()
- alter an array by adding or removing elements.join(' ')
- take an array and join the elements together as a string
Last updated