In my course of mathematics, my teacher asked me to find combination of k in n, since there were too many examples so I get tired of doing the same thing for all exercises because they were all similar.
So I decided to write an algo to help solve them without having to do the same thing for all of them.
const getCombination = (k, n) =>{
let combination = 0;
if( k < 0 || n < 0){
return "n and k must be positive"
}
combination = k > n ? "n must be greater than or equal to k" : getFactoriel(n) / (getFactoriel(n-k)*getFactoriel(k))
return combination
}
const getFactoriel = x =>{
if(x < 0){
return `${x} is negative, x can only be positive or null`
}else if(x === 0 || x === 1){
return 1
}else{
return x*getFactoriel(x-1)
}
}
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
describe("Solution", function() {
it("should test for something", function() {
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
});
});