michael-h.dk

Multimedia Designer and Web Developer

Photo by Samuel Zeller from Unsplash.com

Array Cardio 2

From the Wikipedia article:

In computer science, an array data structure, or simply an array, is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored such that the position of each element can be computed from its index tuple by a mathematical formula. The simplest type of data structure is a linear array, also called one-dimensional array.

In all programming languages there are multiple ways to work with an array. Most of the time it is happening invisible to the user, but here I will try to show how you can use the methods of working with arrays in JavaScript.

Birth years

For the first exsamples I am using a list of names and birth years. You are welcome to enter some examples of your own if you want. None of the data you enter will be saved anywhere, it is for your use only

1. Array.prototype.some()

From MDN web docs:

The some() method tests whether at least one element in the array passes the test implemented by the provided function.

Check to see if any in the array is over a certain age entered by the user.

const answer = people.some(person => ((new Date().getFullYear()) - person.year >= inputAge);

2. Array.prototype.every()

From MDN web docs:

The every() method tests whether all elements in the array pass the test implemented by the provided function.

Check to see if any in the array is over a certain age entered by the user.

const answer = people.some(person => ((new Date().getFullYear()) - person.year >= inputAge);