Northcentral Technical College Introduction To Programming
10-152-310
Introduction to Programming with JavaScript NTC 10-152-310

Learning Plan 6 Parallel Arrays

# Parallel Arrays

#### File: /Learning Plan 6/parallelArrays.html

- Altogether now!

### Arrays in Parallel

- Until we have full Object-Orientation to work with we need to have data that that is more rich than just one array.
- One common practice is `parallel arrays`.
- With this technique we use more than one array and have the same number of elements in each one. The data in the arrays relates to the data in the other arrays.
- I think only an example will work here!
- Here we have one array with a few states, and we have another array of state capitals. Notice that they are in the same order! The first element in the state array is "Alabama" and the first element in the capitals array is the capital of Alabama, "Montgomery".
- This is a very handy arrangement!

- Because the arrays are the same length, we can use the `length` of either one of them to run the loop test.
- The magic is that inside the loop the `index` refers to the same position in each array!
- Would a picture help?

- This lets us do many neat tricks. Here are some possiblities:
  - Display the full state name when our data only has the abbreviation.
  - Expand a month from `12` to `December`.
  - Associate an inventory item with a stock amount number.
  - Search though one of the arrays and output the data from the other. We can convert data with this trick.

### Lab Time

- 6.2 Lab 3