|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Array Cardio 💪</title> |
| 6 | + <script src="data.js"></script> |
| 7 | +</head> |
| 8 | +<body> |
| 9 | + |
| 10 | +<p><em>Psst: have a look at the JavaScript Console</em> 💁</p> |
| 11 | + |
| 12 | +<script> |
| 13 | + // Array.prototype.filter() |
| 14 | + // 1. Filter the list of inventors for those who were born in the 1500's |
| 15 | + console.log( |
| 16 | + inventors.filter( ( { year } ) => ( 1499 < year && year < 1600 ) ) |
| 17 | + ); |
| 18 | + |
| 19 | + // Array.prototype.map() |
| 20 | + // 2. Give us an array of the inventors' first and last names |
| 21 | + console.log( |
| 22 | + inventors.map( ( { first, last } ) => `${first} ${last}` ) |
| 23 | + ); |
| 24 | + |
| 25 | + // Array.prototype.sort() |
| 26 | + // 3. Sort the inventors by birthdate, oldest to youngest |
| 27 | + console.log( |
| 28 | + inventors.sort( ( { year: a }, { year: b } ) => ( a - b ) ) |
| 29 | + ); |
| 30 | + |
| 31 | + // Array.prototype.reduce() |
| 32 | + // 4. How many years did all the inventors live? |
| 33 | + console.log( |
| 34 | + inventors.reduce( ( sum, { year, passed } ) => ( sum + ( passed - year ) ), 0 ) |
| 35 | + ); |
| 36 | + |
| 37 | + // 5. Sort the inventors by years lived |
| 38 | + console.log( |
| 39 | + inventors.sort( ( a, b ) => ( ( a.passed - a.year ) - ( b.passed - b.year ) ) ) |
| 40 | + ); |
| 41 | + |
| 42 | + // 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name |
| 43 | + // https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris |
| 44 | + // TODO: Paste the following code in your console on the Wikipedia page: |
| 45 | +// Array.from( document.querySelector( '.mw-category' ).getElementsByTagName( 'a' ) ) |
| 46 | +// .map( ( el ) => el.text ) |
| 47 | +// .filter( ( name ) => name.includes( 'de' ) ); |
| 48 | + |
| 49 | + // 7. sort Exercise |
| 50 | + // Sort the people alphabetically by last name |
| 51 | + console.log( |
| 52 | + people.sort( ( a, b ) => ( a.split( ',' )[0] > b.split( ',' )[0] ) ) |
| 53 | + ); |
| 54 | + |
| 55 | + // 8. Reduce Exercise |
| 56 | + // Sum up the instances of each of these |
| 57 | + console.log( |
| 58 | + data.reduce( ( sums, item ) => { |
| 59 | + sums[ item ] = ( sums[ item ] || 0 ) + 1; |
| 60 | + |
| 61 | + return sums; |
| 62 | + }, {} ) |
| 63 | + ); |
| 64 | +</script> |
| 65 | + |
| 66 | +</body> |
| 67 | +</html> |
0 commit comments