-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcardio.js
147 lines (132 loc) Β· 3.41 KB
/
cardio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// ππΌππΌππΌππΌ
/**
* Filters people array based on total number of letters in their name (including spaces).
* Filter if name is strictly greater than length
*
* @param {string[]} people
* @param {number} length
* @returns {string[]} filtered array
*/
function filterByLength(people, length) {
return people.filter(people => people.length > length);
}
/**
* Returns an array of every nth person.
* Note that counting starts
* at 0,
* so the returned array will always include (at least) the first person.
*
* @param {string[]} people
* @param {number} n
* @returns {string[]}
*
* @example
* everyNPerson(['Matt', 'Kim', 'Kanye', 'Obama', 'Hans'], 2)
* // β ['Matt', 'Kanye', 'Hans']
*/
function everyNPerson(people, n) {
if (n === 0) {
return [...people];
}
return people.filter((_, i) => i % n === 0 || n === 0);
}
/**
* Returns an array where each entry is the person's intials
* @param {string[]} people
* @returns {string[]} intials array
*
* @example
* initials(['Kanye West', 'Barack Obama'])
* // β ['KW', 'BO']
*/
function initials(people) {
// const newArr = [];
const people2 = people.map(person => {
const fullName = person.split(' ');
const [first, last] = fullName;
return `${first} ${last}`;
});
}
/**
* Returns an array where every person is prepended with their position in the array
* @param {string[]} people
* @returns {string[]}
*
* @example
* peopleWithPosition(['Kanye', 'Barack'])
* // β ['1. Kanye', '2. Barack']
*/
function peopleWithPosition(people) {
return people.map((name, index) => `${index}: ${name}`);
}
/**
* Sorts `people` by first name
* @param {string[]} people
* @returns {string[]} sorted array
*/
function sortByFirstName(people) {
return people.concat().sort();
// return [...people].sort();
}
/**
* Sorts `people` by last name
* @param {string[]} people
* @returns {string[]} sorted array
*/
function sortByLastName(people) {
const sortedArray = [...people];
return sortedArray.sort((a, b) => {
const alastName = a.split(' ')[1];
const blastName = b.split(' ')[1];
if (alastName > blastName) {
return 1;
}
return -1;
});
// const people2 = [...people];
// return people2.sort((left, right) => left.split(' ')[1] > right.split(' ')[1] ? 1 : -1);
}
/**
* Counts all the characters in the people array (including spaces)
* @param {Array} people Array of names
* @return Number of characters
*/
function countTotalCharacters(people) {
return people.reduce((arr, current) => arr + current.length, 0);
}
/**
* Returns `true` if everyone in `people` has `letter` in their name.
* Returns `false` otherwise
* @param {string[]} people
* @param {string} letter
* @returns {boolean}
*/
function everyoneHasLetter(people, letter) {
return people.every(person => person.includes(letter));
// const returnValue = false;
// people.forEach(names => {
// if (!names.includes(letter)) {
// return false;
// }
// });
// return true;
}
/**
* Returns `true` if at least one person has `letter` in their name.
* Returns `false` otherwise
* @param {string[]} people
* @param {string} letter
* @returns {boolean}
*/
function someoneHasLetter(people, letter) {}
module.exports = {
filterByLength,
everyNPerson,
initials,
sortByFirstName,
sortByLastName,
countTotalCharacters,
everyoneHasLetter,
someoneHasLetter,
peopleWithPosition,
};