Array Methods Practice

Almost every array methods have four things in common, they must have : 1. Purpose - forEach() executes a function for each array elements, Its primarily used to iterate over arrays without needing to manually manage loop counters. 2. Parameters - It takes up to three parameters, currentValue (required), index and current array. 3. Return - It always returns undefined, so it cannot be chained with other methods. 4. Usages/Notes/Syntax - Doesn't modify the original array. Array.forEach(function(...params){});
1. Array.prototype. filter()

Filter the list of inventors for those who were born in the 1500

Solution:

const under1600 = inventors.filter( (e) => e.year >= 1500 && e.year < 1600 );

2. Array.prototype.map()

Give us an array of the inventory first and last names

Solution:

const fullNames = inventors.map((e) => e.first +' '+ e.last );

3. Array.prototype.sort()

Sort the inventors by birth date, oldest to youngest

Solution:

const sortedDate = inventors.sort((a, b) => { return a.year - b.year });

4. Array.prototype. reduce()

How many yeans did all the inventors live?

Solution:

const lived = inventors.reduce((acc, n) => { let total = n.passed - n.year; return acc + total; }, 0);

const sep = inventors.map(e => { return `${e.first} ${e.last} lived ${e.passed - e.year} years`; });

S. Sort the inventors by years lived
Solution:

const ageSorted = inventors.sort((a, b) => { return b.passed - b.year - (a.passed - a.year); }); console.table(ageSorted);

console.log( ageSorted.map((v) => { return v.passed - v.year; }) );

6. Create a list of Boulevards in Paris that contain 'de' anywhere in the name

https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris

Solution:

const category = document.querySelector(".mw-category");
const links = category.querySelectorAll("a");

const de = links .map(link => link.textContent) .filter(streetName => streetName.includes('de'));

7. Sort Exercise

Sort the people alphabetically by last name

Solution:

const mapped = people.map((elem) => { return elem.split(", ").reverse().toString().trim(); }); console.table(mapped.sort());

8. Reduce Exercise

Sum up the instances of each of these

Solution:

const occur = data.reduce((obj,item)=> { obj[item] = (obj[item] || 0) + 1; return obj; },{}); console.table(occur);

9. some() Exercise

At least one element has to match to return true else false.

Solution:

const isAdult = people.some((person) => { const currentYear = new Date().getFullYear(); return currentYear - person.year >= 19; }); console.log(isAdult);

10. every() Exercise

It returns true if every elements match else false.

Solution:

const allAdults = people.every((person) => { const currentYear = new Date().getFullYear(); return currentYear - person.year >= 19; }); console.log(allAdults);

11. find() Exercise

Find is like filter, but instead returns just the one you are looking for

Find the comment with the ID of 823423

Solution:

const comment = comments.find((comment) => { return comment.id === 823423; }); console.log(comment);

11. findIndex() Exercise

Find the comment with this ID

Delete the comment with the ID of 823423

Solution:

const comment = comments.findIndex((comment) => { return comment.id === 823423; }); console.log(comment);