diff --git a/README.md b/README.md
index 0f083e4a2..0b99c993a 100644
--- a/README.md
+++ b/README.md
@@ -260,6 +260,7 @@ average(1, 2, 3);
* [`average`](#average)
* [`averageBy`](#averageby)
+* [`binomialCoefficient`](#binomialcoefficient)
* [`clampNumber`](#clampnumber)
* [`degreesToRads`](#degreestorads)
* [`digitize`](#digitize)
@@ -4361,6 +4362,41 @@ averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 5
[⬆ Back to top](#table-of-contents)
+### binomialCoefficient
+
+Evaluates the binomial coefficient of two integers `n` and `k`.
+
+Use `Number.isNaN()` to check if any of the two values is `NaN`.
+Check if `k` is less than `0`, greater than or equal to `n`, equal to `1` or `n - 1` and return the appropriate result.
+Check if `n - k` is less than `k` and switch their values accordingly.
+Loop from `2` through `k` and calculate the binomial coefficient.
+Use `Math.round()` to account for rounding errors in the calculation.
+
+```js
+const binomialCoefficient = (n, k) => {
+ if (Number.isNaN(n) || Number.isNaN(k)) return NaN;
+ if (k < 0 || k > n) return 0;
+ if (k === 0 || k === n) return 1;
+ if (k === 1 || k === n - 1) return n;
+ if (n - k < k) k = n - k;
+ let res = n;
+ for (let j = 2; j <= k; j++) res *= (n - j + 1) / j;
+ return Math.round(res);
+};
+```
+
+
+Examples
+
+```js
+binomialCoefficient(8, 2); // 28
+```
+
+
+
+ [⬆ Back to top](#table-of-contents)
+
+
### clampNumber
Clamps `num` within the inclusive range specified by the boundary values `a` and `b`.
diff --git a/docs/index.html b/docs/index.html
index 7aa7f51ad..45e2c52bc 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -50,7 +50,7 @@
scrollToTop();
}
}, false);
- }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Adapter
ary
Creates a function that accepts up to n arguments, ignoring any additional arguments.
Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).
Evaluates the binomial coefficient of two integers n and k.
Use Number.isNaN() to check if any of the two values is NaN. Check if k is less than 0, greater than or equal to n, equal to 1 or n - 1 and return the appropriate result. Check if n - k is less than k and switch their values accordingly. Loop from 2 through k and calculate the binomial coefficient. Use Math.round() to account for rounding errors in the calculation.
constbinomialCoefficient=(n, k)=>{
+ if(Number.isNaN(n)|| Number.isNaN(k))returnNaN;
+ if(k <0|| k > n)return0;
+ if(k ===0|| k === n)return1;
+ if(k ===1|| k === n -1)return n;
+ if(n - k < k) k = n - k;
+ let res = n;
+ for(let j =2; j <= k; j++) res *=(n - j +1)/ j;
+ return Math.round(res);
+};
+
binomialCoefficient(8,2);// 28
clampNumber
Clamps num within the inclusive range specified by the boundary values a and b.
If num falls within the range, return num. Otherwise, return the nearest number in the range.
constclampNumber=(num, a, b)=> Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));