-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinBy.js
More file actions
37 lines (27 loc) · 787 Bytes
/
MinBy.js
File metadata and controls
37 lines (27 loc) · 787 Bytes
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
function minBy(array, iteratee) {
let computed; let result;
for (const value of array) {
const current = iteratee(value)
if (current != null && (computed === undefined || current < computed)) {
computed = current
result = value
}
}
return result
}
console.log(minBy([2, 3, 1, 4], (num) => num))
console.log(minBy([{ n: 1 }, { n: 2 }], (o) => o.n))
function functionLength (func) {
return func.length
}
function foo() {}
function bar(a) {}
function baz(a, b) {}
console.log(functionLength(foo)); // 0
functionLength(bar); // 1
console.log(functionLength(baz)); // 2
function clamp (value, lower, upper) {
return Math.min(upper, (Math.max(value, lower)))
}
console.log(clamp(-10, -3, 5))
console.log(clamp(3, 0, 5))