1.4 KiB
1.4 KiB
title | id | challengeType | forumTopicId |
---|---|---|---|
Evaluate binomial coefficients | 598de241872ef8353c58a7a2 | 5 | 302259 |
Description
Instructions
Tests
tests:
- text: <code>binom</code> should be a function.
testString: assert(typeof binom === 'function');
- text: <code>binom(5,3)</code> should return 10.
testString: assert.equal(binom(5, 3), 10);
- text: <code>binom(7,2)</code> should return 21.
testString: assert.equal(binom(7, 2), 21);
- text: <code>binom(10,4)</code> should return 210.
testString: assert.equal(binom(10, 4), 210);
- text: <code>binom(6,1)</code> should return 6.
testString: assert.equal(binom(6, 1), 6);
- text: <code>binom(12,8)</code> should return 495.
testString: assert.equal(binom(12, 8), 495);
Challenge Seed
function binom(n, k) {
}
Solution
function binom(n, k) {
let coeff = 1;
for (let i = n - k + 1; i <= n; i++) coeff *= i;
for (let i = 1; i <= k; i++) coeff /= i;
return coeff;
}