I wrote three javascript functions which works the same task. They all provide the same result but I am wondering which one of them is faster and most scalable?
Here are my functions first one
function odds(n, p) {
var acc = 1;
for (var i = 0; i < n; i++) {
acc *= (n - i) / (p - i);
}
return acc;
}
second one
function odds(n, p) {
if (n == 0) {
return 1;
} else {
return (n / p) * odds(n - 1, p - 1);
}
}
third one
var odds = (function () {
var odds1 = function(n, p, acc) {
if(n == 0) {
return acc;
} else {
return odds1(n - 1, p - 1, (n / p) * acc);
}
}
return function(n, p) {
return odds1(n, p, 1);
}
}());