Javascript - Arrays
Learning Goals
Creating an array
index
.length
Array methods
pushindexOf
Iterating through an array
forEachfor of
Using conditional logic on each data element from an array
Flipped classroom videos
Javascript Arrays Made Simple: Iterators (Part 1) (first 7 min)
Teacher instruction
Nye grupper idag. Folk der før har programmeret og folk der ikke har
Kom med til Copenhagen JS d. 19/10. Der er mad og drikke! Link her
Studiekort? https://education.github.com/pack
Lav opgaver, men hop over opgave 5, 6 og 7. Dem laver vi i klassen sammen
Vi mødes 15:30 hvor jeg vil løse en af opgaverne og vise den "rigtige" løsning
Peer instruction
Question 1
How many times does this loop print "Hey everyone"?
4
5
6
Syntax error
Question 2
How many times does this loop print "Hey everyone"?
0
4
5
Syntax error
Creating an array
If you ever find yourself writing code like this...
...then it's probably time to use an array!
Arrays are data structures that hold a list of values. We call these values the elements of the array.
Arrays can hold any type of value (although almost always you only have one data type per array).
Index
An array is based on an index. The first element is saved at index 0

Accessing elements using index
You can access elements in an array using the index of an element with bracket notation. If you want the first element you use index 0 which can cause some confusion 😱
🔔 Remember: All arrays start at position 0! To access the first element in an array, you need to access index 0, the second element at 1. There are some very intense reasons for this, but most people just accept it and move on.
Assigning elements
You can also assign new values to parts of an array:
.length
.lengthTo get the number of elements in an array you can use the .length property
📝 Exercise 1 - level 1
Create an array that contains a list of your interests. Get the number of interests you have added to the list. Give the variable a good name
Log out the following string I have 10 interests
📝 Exercise 2 - level 1
Using the following array
Now, using the correct indexes, print the following values from the array:
strawberry
kiwi
orange
banana
taken from https://syllabus.codeyourfuture.io/js-core-1/week-2/lesson#arrays
📝 Exercise 3 - level 2
Write some code that can get the first and last elements in an array.
Array methods
There are lots of different array methods. Today we will focus the two most important ones
.push
.pushElements can be "pushed" into an array. The pushed element will figure as the latest element.
.indexOf
.indexOfIf you know the value and want the index number, you can use indexOf
More methods documented here
📝 Exercise 4 - Favorite Songs - level 2
Create an array called
favoriteSongsand add five of your favorite song titles.Use the
.pushmethod to add another song to the end of the array.Use the
indexOfmethod to find the position of a song in the array.
Iterating an array
Iterating an array is about going through each element in an array. Here is an example
For...of
ForEach
Does the same, but we can get the index aswell!
📝 Exercise 5 - teachers and students
Duration: 20 min
In the following exercise one group will randomly be selected to be teachers and the other group will be students
In groups of two people prepare a small 5 minute lecture. The lecture should explain the topic of forEach or for...of any way you like. That might be with a small slideshow or it might be with code, thats up to you.
As teachers present the 5 minute lecture
As students ask good interesting questions
📝 Exercise 6 - In class let's analyze this code
📝 Exercise 7 - Analyze this code
Ask the following questions
What does this code do?
How would you improve it?
Does it work?
Do you like the variable names?
Is the code easy to understand?
📝 Exercise 8 - level 2
Given the following array:
Write a program that prints out each word and its length.
Here is the expected output:
📝 Exercise 9 - level 2
Given the following array:
Write code that finds the index of 'bird' in the array and then replaces it with 'parrot'. Print the updated array.
Expected output: ['cat', 'dog', 'parrot', 'fish', 'elephant']
📝 Exercise 10 - level 3
Given the following array:
Write code that
Calculates the total grades from the array
Calculates the average grades in the the array
Expected output: Average grade is 86
📝 Exercise 11 - level 3
Using the following array:
Write code that prints the words that have more than 5 letters.
Expected output: banana cherry
📝 Exercise 12 - level 3: Data Filtering
You are given an array of user input data. Each item in the array is a string that a user has entered into a search bar on a website.
Clean the data: Remove any leading or trailing whitespace from each item.
Standardize the data: Convert all items to lowercase so that the search is case-insensitive.
Remove any duplicate items from the array.
Print the cleaned and standardized array
Expected output: ['apple', 'banana', 'cherry']
Last updated