3.5 - Arrays

Today's Lesson & Task: 3.5 - Arrays

  • Today's material is in Replit but it is pasted below as well.


Imagine creating a contact list program. Every contact that gets added is another piece of data that has to go in a variable. But if you don't know how many contacts there will be - how do you know how many variables to make? contact1, contact2, ... contact999?

Instead, we can package data together in one variable - just like packaging characters in a String! Strings are a very special type of variable. They are an array. Almost every programming language in the world has Arrays - at least as an add-on. Arrays let us package multiple values together in one variable. Kind of like a string of letters but instead it could be numbers or booleans or Strings.


// An array of numbers

let someNumbers = [2,4,5,0,-9,5,2,1,2,4];

console.log(someNumbers.length); // 10

One of the biggest differences between Strings and Arrays - you can modify an array.

console.log(someNumbers[1]); // 4

someNumbers[1] = 17;

console.log(someNumbers[1]); // 17

In JavaScript, an Array can contain a mixture of any type of data.

let myArray = ["This is text", 4, 5, true];

We can loop through arrays just like we did with Strings!

for (let i = 0; i < myArray.length; i++)


We can add an element to the end of an existing array with .push()

myArray.push("Some more text");

We can also remove the last element with .pop()

We can declare an empty array:

let myArrayForLater = [];

There are other built-in functions as well, that you can look up online.

Your Task

  1. Create a function printArray(arr) that prints the elements of arr to the console one-by-one, each on a new line.

  2. Create a function minMax(arr) that goes through a numeric array and returns an array of [min, max] where min is the lowest and max the highest numeric values in the array. It's safe to assume the input parameter is all numbers and has at least one value.

let myArray = [7,2,-4,5,2,9,8,0,1,3,9,-5,-1,5,-1,-8,2,3];

minMax(myArray); // returns [-8, 9]

minMax([5,5,5,5,5,5,5]); // returns [5, 5]

  1. Create a function sum(arr) that returns the sum of only the numeric values in the array. BTW - a boolean "true" equates to 1, and that's ok. Hint: we had learned the isNaN( ) function in previous work.

sum([1,2,3,4,5]); // returns 15

let x = ["Hello", "4", 3, true];

sum(x); // returns 8 because of "4", 3, and true

Hint: for this you might need such things as isNaN, parseInt or Number, and typeof.

  1. Create a function contains(arr, value) that returns true if the arr contains a copy of the given value. It should return false otherwise.

let someValues = [1, 2, 3, 4, "hello"];

contains(someValues, "hello"); // Returns true

contains(someValues, 5); // Returns false

contains(someValues, "HELLO"); // Returns false

contains(someValues, 2); // Returns true

contains(someValues, "2"); // Returns false

Here's some info: The double-equals only compares value. The triple-eqals also compares type.

2 == "2" // true

2 === "2" // false

If you finish early - move on to the Unit 3 Summative Task.