Skip to content

Commit 33595da

Browse files
committed
Add well known examples
1 parent ae1458e commit 33595da

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

JavaScript/4-well-known.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
const persons = [
4+
{ name: 'Marcus Aurelius', city: 'Rome', born: 121 },
5+
{ name: 'Victor Glushkov', city: 'Rostov on Don', born: 1923 },
6+
{ name: 'Ibn Arabi', city: 'Murcia', born: 1165 },
7+
{ name: 'Mao Zedong', city: 'Shaoshan', born: 1893 },
8+
{ name: 'Rene Descartes', city: 'La Haye en Touraine', born: 1596 },
9+
];
10+
11+
console.group('Sort strategy: descending by born year');
12+
{
13+
const strategy = (a, b) => (a.born < b.born ? 1 : -1);
14+
persons.sort(strategy);
15+
console.table(persons);
16+
}
17+
console.groupEnd();
18+
19+
console.group('Sort strategy: ascending by name alphabetically');
20+
{
21+
const strategy = (a, b) => (a.name > b.name ? 1 : -1);
22+
persons.sort(strategy);
23+
console.table(persons);
24+
}
25+
console.groupEnd();
26+
27+
console.group('Filter strategy: single word city name');
28+
{
29+
const strategy = person => person.city.split(' ').length === 1;
30+
const res = persons.filter(strategy);
31+
console.table(res);
32+
}
33+
console.groupEnd();
34+
35+
console.group('Reduce strategy: sum born years');
36+
{
37+
const strategy = (acc, person) => acc + person.born;
38+
const res = persons.reduce(strategy, 0);
39+
console.table(res);
40+
}
41+
console.groupEnd();

0 commit comments

Comments
 (0)