generated from jfarmer/template-javascript
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmaxBy.js
31 lines (28 loc) · 969 Bytes
/
maxBy.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
/**
* Given a `collection` and a function `fn`, returns the **largest** element of `collection`
* ranked by `fn`.
*
* For example, if `fn(string)` returns the length of `string` then `maxBy` would return
* the longest string in the collection.
*
* @example
* function strLength(str) {
* return str.length;
* }
*
* maxBy(['a', 'bb', 'cccc', 'ddd'], strLength); // => 'cccc'
*
* @param {any[]} collection - An array containing anything
* @param {function} fn - A function that returns a `number`
* @returns {any} The element `x` in `collection` with the largest value of `fn(x)`.
*/
function maxBy(collection, predicate) {
// This is your job. :)
}
if (require.main === module) {
console.log('Running sanity checks for maxBy:');
// Add your own sanity checks here.
// How else will you be sure your code does what you think it does?
// How can you be sure it's returning the FIRST thing it finds? Does it matter?
}
module.exports = maxBy;