CS133JS Beginning Programming: JavaScript

Arrays

Topics by Week 
1. Intro to JavaScript programming6. Arrays
2. Functions, Operators and Expressions7. Objects and Object Constructors
3. Conditional Statements: if and switch8. Web Page I/O
4. Repetition: while, do while, and for9. Review
5. Midterm Check-Point10. Term Project
11. Final 

Table of Contents

Introduction

 

Q and A

 

Arrays

What are Arrays?

An array represented as a table.
Each row represents an element.
This array contains string values.

indexvalues
0Rey
1Finn
2Han Solo
3C-3PO
4Chewbacca

 

Creating an Array

An array can be created by declaring it and it can optionally be initialized. Initialization means putting some values into elements of the array. When these values are put into the array, the elements are created in memory.

Accessing Array Elements

The code shown below will access the array element with an index of 2 and assign the value stored there, "Han Solo", to the constant character.

const character = starWars[2];
                    ↑      ↑  
             array name   index 

 

This next line of code will change the value stored at index 3 to "Leia".

starwars[3] = "Leia";

Question: Which name got replaced?

Performing Operations on an Array

 

Practice

  1. Create an array of planets1.
    (Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto.)

  2. Write a function that returns a planet by its number (1 for Mercury, 9 for Pluto).

  3. Write a function that lets you change the name of a planet.

     

Use Arrays in Loops

for loop

We can use a for loop to display the contents of an array. Look at the loop below, assuming we have defined the original starWars array above.

for...of loop

The for...of2 loop has a hidden, "behind the scenes", index variable that starts at zero and is automatically incremented and used to sequentially get all the values in the array.

In a for...of loop, it's best to get the values from the elements into a variable declared with const rather than let.

What do you think is the reason?

while loop

The while loop below iterates as long as the user keeps entering names.

 

Practice

For each of the practice problems below:

Practice problems:

  1. Write a function to copy one array into another.

  2. Write a function to check to see if an element is in an array.

Nested Arrays, aka 2D Arrays

Nested arrays are arrays of arrays. These are also known as 2D arrays.

Example
Certificates in the Software Development and Network Operations degree programs:

You can visualize this 2D array as a table:

2nd index
1st index
012
0"Mobile App Development""Front End Web Devevlopment""Database Specialist"
1"Computer Network Monitoring and Mangement"  

Example
grid for a tic-tac-toe game:

This can be visualized as a table:

 

2nd index
1st index
012
0"x"""""
1"""x"""
2"""""x"

 

Practice

  1. Write a function to let you put an X or O into a particular location in the grid

  2. Write a function to output the grid to the console.

 

Next class: Using array methods

 

Reference

Understanding Arrays in JavaScript by Tania Rascia

JavaScript Array Tutorial on W3Schools

JavaScript Array Tutorial on JavaScriptTutorial.net

 


Creative Commons License Beginning JavaScript Lecture Notes by Brian Bird 2018, revised fall are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.


1 const planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"]
2 The JavaScript for...of loop is similar to the for...each loop in other languages like C++, C#, Java or Python. JavaScript also has a ForEach() array method, but it has a different purpose.