Ad

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)
  }
}