Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cf97d4b

Browse files
committed
Solve all parts of Exercise 4
1 parent 40cf9a4 commit cf97d4b

File tree

2 files changed

+146
-1
lines changed

2 files changed

+146
-1
lines changed

04 - Array Cardio Day 1/script.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
// Get your shorts on - this is an array workout!
22
// ## Array Cardio Day 1
3+
var __assign = (this && this.__assign) || function () {
4+
__assign = Object.assign || function(t) {
5+
for (var s, i = 1, n = arguments.length; i < n; i++) {
6+
s = arguments[i];
7+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
8+
t[p] = s[p];
9+
}
10+
return t;
11+
};
12+
return __assign.apply(this, arguments);
13+
};
314
// Some data we can work with
415
var inventors = [
516
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
@@ -16,19 +27,78 @@ var inventors = [
1627
{ first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
1728
];
1829
var people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black, Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William'];
30+
var firstXChars = function (firstNumber) { return function (input) { return input.toString().slice(0, firstNumber); }; };
31+
var test = function (a, b) { return (a === b); };
32+
var firstTwoChars = firstXChars(2);
1933
// Array.prototype.filter()
2034
// 1. Filter the list of inventors for those who were born in the 1500's
35+
var bornIn1500s = function (person) { return test(firstTwoChars(person.year), '15'); };
36+
var inventorsBornIn1500s = inventors.filter(bornIn1500s);
37+
console.log("inventorsBornIn1500s");
38+
console.table(inventorsBornIn1500s);
2139
// Array.prototype.map()
2240
// 2. Give us an array of the inventors' first and last names
41+
var combineName = function (person) { return person.first + " " + person.last; };
42+
var inventorsNames = inventors.map(combineName);
43+
console.log({ inventorsNames: inventorsNames });
2344
// Array.prototype.sort()
2445
// 3. Sort the inventors by birthdate, oldest to youngest
46+
/**
47+
* Sort function builder
48+
* @param callback
49+
*/
50+
var sortCallback = function (callback) {
51+
return function (compareType) {
52+
return function (a, b) { return compareType(callback(a), callback(b)); };
53+
};
54+
}; // Last, perform the actual comparison
55+
var sortNumberAsc = function (a, b) { return a - b; };
56+
var sortKey = function (key) { return sortCallback(function (value) { return value[key]; }); };
57+
var sortYearsAsc = sortKey('year')(sortNumberAsc);
58+
var inventorsSortedByBirthdate = inventors.sort(sortYearsAsc);
59+
console.log("inventorsSortedByBirthdate");
60+
console.table(inventorsSortedByBirthdate);
2561
// Array.prototype.reduce()
2662
// 4. How many years did all the inventors live?
63+
var personLifespan = function (person) { return person.passed - person.year; };
64+
var personArrayReducer = function (acc, currVal) { return acc + personLifespan(currVal); };
65+
var totalInventorLifespan = inventors.reduce(personArrayReducer, 0);
66+
console.log({ totalInventorLifespan: totalInventorLifespan });
2767
// 5. Sort the inventors by years lived
68+
var appendLifespan = function (person) { return (__assign({ lifespan: personLifespan(person) }, person)); };
69+
var sortByLifespan = sortCallback(personLifespan)(sortNumberAsc);
70+
var inventorsSortedByYearsLived = inventors
71+
// .map(appendLifespan)
72+
.sort(sortByLifespan);
73+
console.log("inventorsSortedByYearsLived");
74+
console.table(inventorsSortedByYearsLived);
2875
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
2976
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
77+
var $ = function (selector) { return [].slice.call(document.querySelectorAll(selector)); };
78+
var containsText = function (text) { return function (element) { return element.innerText.indexOf(text) > -1; }; };
79+
var containsDe = containsText('de');
80+
var boulevardsWithDe = $('.mw-category-group a').filter(containsDe);
81+
console.log(boulevardsWithDe);
3082
// 7. sort Exercise
3183
// Sort the people alphabetically by last name
84+
var sortAlphaAsc = function (a, b) { return a > b ? 1 : a < b ? -1 : 0; };
85+
var sortByFirstElement = sortCallback(function (array) { return array[0]; });
86+
var sortByFirstElementAlpha = sortByFirstElement(sortAlphaAsc);
87+
var splitOnComma = function (string) { return string.split(','); };
88+
var joinWithComma = function (array) { return array.join(', '); };
89+
var peopleByLastName = people
90+
.map(splitOnComma)
91+
.sort(sortByFirstElementAlpha)
92+
.map(joinWithComma);
93+
console.table(peopleByLastName);
3294
// 8. Reduce Exercise
3395
// Sum up the instances of each of these
3496
var data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck'];
97+
var dataCount = data.reduce(function (acc, currVal) {
98+
if (!acc[currVal]) {
99+
acc[currVal] = 1;
100+
}
101+
acc[currVal] = acc[currVal] + 1;
102+
return acc;
103+
}, {});
104+
console.table(dataCount);

04 - Array Cardio Day 1/script.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,102 @@ const inventors = [
2020

2121
const people = ['Beck, Glenn', 'Becker, Carl', 'Beckett, Samuel', 'Beddoes, Mick', 'Beecher, Henry', 'Beethoven, Ludwig', 'Begin, Menachem', 'Belloc, Hilaire', 'Bellow, Saul', 'Benchley, Robert', 'Benenson, Peter', 'Ben-Gurion, David', 'Benjamin, Walter', 'Benn, Tony', 'Bennington, Chester', 'Benson, Leana', 'Bent, Silas', 'Bentsen, Lloyd', 'Berger, Ric', 'Bergman, Ingmar', 'Berio, Luciano', 'Berle, Milton', 'Berlin, Irving', 'Berne, Eric', 'Bernhard, Sandra', 'Berra, Yogi', 'Berry, Halle', 'Berry, Wendell', 'Bethea, Erin', 'Bevan, Aneurin', 'Bevel, Ken', 'Biden, Joseph', 'Bierce, Ambrose', 'Biko, Steve', 'Billings, Josh', 'Biondo, Frank', 'Birrell, Augustine', 'Black, Elk', 'Blair, Robert', 'Blair, Tony', 'Blake, William'];
2222

23+
interface Person {
24+
first: string;
25+
last: string;
26+
year: number;
27+
passed: number;
28+
}
29+
30+
const firstXChars = (firstNumber: number) => (input: string | number) => input.toString().slice(0, firstNumber);
31+
const test = (a, b) => (a === b);
32+
33+
const firstTwoChars = firstXChars(2);
34+
2335
// Array.prototype.filter()
2436
// 1. Filter the list of inventors for those who were born in the 1500's
37+
const bornIn1500s = (person: Person) => test(firstTwoChars(person.year), '15' )
38+
const inventorsBornIn1500s = inventors.filter(bornIn1500s);
39+
console.log(`inventorsBornIn1500s`);
40+
console.table(inventorsBornIn1500s);
2541

2642
// Array.prototype.map()
2743
// 2. Give us an array of the inventors' first and last names
44+
const combineName = (person: Person) => `${person.first} ${person.last}`;
45+
const inventorsNames = inventors.map(combineName);
46+
console.log({inventorsNames});
2847

2948
// Array.prototype.sort()
3049
// 3. Sort the inventors by birthdate, oldest to youngest
3150

51+
/**
52+
* Sort function builder
53+
* @param callback
54+
*/
55+
const sortCallback =
56+
(callback: (a: any) => any) => // First, curry with callback saved
57+
(compareType: (a: any, b: any) => any) => // Second, curry with comparison function
58+
(a, b) => compareType(callback(a), callback(b)); // Last, perform the actual comparison
59+
60+
const sortNumberAsc = (a: number, b: number) => a - b;
61+
const sortKey = (key: string) => sortCallback(value => value[key]);
62+
const sortYearsAsc = sortKey('year')(sortNumberAsc);
63+
64+
const inventorsSortedByBirthdate = inventors.sort(sortYearsAsc);
65+
console.log(`inventorsSortedByBirthdate`);
66+
console.table(inventorsSortedByBirthdate);
67+
3268
// Array.prototype.reduce()
3369
// 4. How many years did all the inventors live?
70+
const personLifespan = (person: Person) => person.passed - person.year;
71+
const personArrayReducer = (acc, currVal) => acc + personLifespan(currVal);
72+
const totalInventorLifespan = inventors.reduce(personArrayReducer, 0);
73+
console.log({totalInventorLifespan});
3474

3575
// 5. Sort the inventors by years lived
76+
const appendLifespan = (person) => ({
77+
lifespan: personLifespan(person),
78+
...person
79+
})
80+
const sortByLifespan = sortCallback(personLifespan)(sortNumberAsc);
81+
const inventorsSortedByYearsLived = inventors
82+
// .map(appendLifespan)
83+
.sort(sortByLifespan);
84+
85+
console.log(`inventorsSortedByYearsLived`);
86+
console.table(inventorsSortedByYearsLived);
3687

3788
// 6. create a list of Boulevards in Paris that contain 'de' anywhere in the name
3889
// https://en.wikipedia.org/wiki/Category:Boulevards_in_Paris
39-
90+
const $ = (selector) => [].slice.call(document.querySelectorAll(selector));
91+
const containsText = (text: string) => (element: HTMLElement) => element.innerText.indexOf(text) > -1;
92+
const containsDe = containsText('de');
93+
const boulevardsWithDe = $('.mw-category-group a').filter(containsDe);
94+
console.log(boulevardsWithDe);
4095

4196
// 7. sort Exercise
4297
// Sort the people alphabetically by last name
98+
const sortAlphaAsc = (a, b) => a > b ? 1 : a < b ? -1 : 0;
99+
const sortByFirstElement = sortCallback((array: any[]) => array[0]);
100+
const sortByFirstElementAlpha = sortByFirstElement(sortAlphaAsc);
101+
const splitOnComma = (string: string) => string.split(',');
102+
const joinWithComma = (array: any[]) => array.join(', ');
103+
const peopleByLastName = people
104+
.map(splitOnComma)
105+
.sort(sortByFirstElementAlpha)
106+
.map(joinWithComma);
107+
console.table(peopleByLastName);
43108

44109
// 8. Reduce Exercise
45110
// Sum up the instances of each of these
46111
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
112+
113+
const dataCount = data.reduce((acc, currVal) => {
114+
if (!acc[currVal]) {
115+
acc[currVal] = 1;
116+
}
117+
acc[currVal] = acc[currVal] + 1;
118+
return acc;
119+
}, {});
120+
121+
console.table(dataCount);

0 commit comments

Comments
 (0)