Files
CS343-Labs/Prep 4/arrays.html
2025-09-10 14:42:57 -04:00

101 lines
5.4 KiB
HTML
Executable File

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Array Champion 3000</title>
<link rel="icon" href="https://fav.farm/🏆">
</head>
<body>
<p><em>Psst: have a look at the JavaScript Console</em> 💁</p>
<script>
// It's time to work out those array muscles! 💪
// Complete the following exercises to get your array game on point.
// Become an array CHAMPION!!!1!! 🏆🏆🏆
// Some data we can work with
const inventors = [
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
{ first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
{ first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
{ first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
{ first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
{ first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
{ first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
{ first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
{ first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
{ first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
{ first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
];
const people = [
'Bernhard, Sandra', 'Bethea, Erin', 'Becker, Carl', 'Bentsen, Lloyd', 'Beckett, Samuel', 'Blake, William', 'Berger, Ric', 'Beddoes, Mick', 'Beethoven, Ludwig',
'Belloc, Hilaire', 'Begin, Menachem', 'Bellow, Saul', 'Benchley, Robert', 'Blair, Robert', 'Benenson, Peter', 'Benjamin, Walter', 'Berlin, Irving',
'Benn, Tony', 'Benson, Leana', 'Bent, Silas', 'Berle, Milton', 'Berry, Halle', 'Biko, Steve', 'Beck, Glenn', 'Bergman, Ingmar', 'Black, Elk', 'Berio, Luciano',
'Berne, Eric', 'Berra, Yogi', 'Berry, Wendell', 'Bevan, Aneurin', 'Ben-Gurion, David', 'Bevel, Ken', 'Biden, Joseph', 'Bennington, Chester', 'Bierce, Ambrose',
'Billings, Josh', 'Birrell, Augustine', 'Blair, Tony', 'Beecher, Henry', 'Biondo, Frank'
];
// Array.prototype.filter()
// 1. Filter the list of inventors for those who were born in the 1500's
// Store the result in a variable called 'fifteen'
const fifteen = inventors.filter((inventor) => inventor.year >= 1500 && inventor.year < 1600)
// Array.prototype.map()
// 2. Give us an array of the inventors first and last names
// Store the result in a variable called 'fullNames'
const fullNames = inventors.map((inventor) => `${inventor.first} ${inventor.last}`)
// Array.prototype.toSorted()
// 3. Sort the inventors by birthdate, oldest to youngest
// Store the result in a variable called 'ordered'
const ordered = inventors.toSorted((previous, current) => previous.year - current.year)
// Array.prototype.reduce()
// 4. How many years did all the inventors live all together?
// Store the result in a variable called 'totalYears'
const totalYears = inventors.reduce((previous, current) => previous + (current.passed - current.year), 0)
// 5. Sort the inventors by years lived
// Store the result in a variable called 'oldest'
const oldest = inventors.toSorted((previous, current) => (current.passed - current.year) - (previous.passed - previous.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
// Store the result in a variable called 'category'
const boulevards = [
'Boulevards of Paris', 'City walls of Paris', 'Thiers wall', 'Wall of Charles V', 'Wall of Philip II Augustus', 'City gates of Paris',
"Haussmann's renovation of Paris", 'Boulevards of the Marshals', 'Boulevard Auguste-Blanqui', 'Boulevard Barbès', 'Boulevard Beaumarchais',
"Boulevard de l'Amiral-Bruix", 'Boulevard Mortier', 'Boulevard Poniatowski', 'Boulevard Soult', 'Boulevard des Capucines',
'Boulevard de la Chapelle', 'Boulevard de Clichy', 'Boulevard du Crime', "Boulevard du Général-d'Armée-Jean-Simon", 'Boulevard Haussmann',
"Boulevard de l'Hôpital", 'Boulevard des Italiens', 'Boulevard Lefebvre', 'Boulevard de la Madeleine', 'Boulevard de Magenta',
'Boulevard Malesherbes', 'Boulevard Marguerite-de-Rochechouart', 'Boulevard Montmartre', 'Boulevard du Montparnasse', 'Boulevard Raspail',
'Boulevard Richard-Lenoir', 'Boulevard Saint-Germain', 'Boulevard Saint-Michel', 'Boulevard de Sébastopol', 'Boulevard de Strasbourg',
'Boulevard du Temple', 'Boulevard Voltaire', 'Boulevard de la Zone']
const category = boulevards.filter((boulevard) => /de/.test(boulevard))
// 7. Sorting exercise
// Sort the people alphabetically by last name
// Store the result in a variable called 'alpha'
const alpha = people.toSorted((previous, current) => previous.split(", ")[0].localeCompare(current.split(", ")[0]))
// 8. Reduce Exercise
// Sum up the instances of each of these:
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck'];
// Store the result in a variable called 'transportation'
// The result should be an object where the keys are the names of the transportation methods and the values are the number of instances
const transportation = data.reduce((obj, current) => Object.assign(obj, { [current]: (obj[current] ?? 0) + 1 }), {})
</script>
</body>
</html>