programming-examples/js/Math/Calculate the falling factorial of a number.js
2019-11-15 12:59:38 +01:00

19 lines
354 B
JavaScript

function fallingFactorial(n, k)
{
var i = (n - k + 1),
r = 1;
if (n < 0)
{
throw new Error("n must be positive.");
}
if (k > n)
{
throw new Error("k cannot be greater than n.");
}
while (i <= n)
{
r *= i++;
}
return r;
}
console.log(fallingFactorial(10, 2));