Combinations & Permutations Calculator
by Daniel Echeverria
HTML
<p><strong>Calculate: </strong>
Permutations <input type="radio" onclick="calc();" id="p" checked="checked" value="Permutations" name="type" onclick="P(n.value,r.value)">
Combinations <input type="radio" onclick="calc();" id="c" value="Combinations" name="type" onclick="C(n.value,r.value)">
</p>
<p>
<input type="text" id="r" onkeyup="calc();" />
Objects<br />
From a Set of
<br />
<input type="text" id="n" onkeyup="calc();"/>
Objects</p>
<div id="answer"></div>
JavaScript
function f(x) {
x == parseInt(x, 10);
if(x==0) {
return 1;
}
return x * f(x-1);
}
function P(n,r){
return "<strong>Permutations:</strong> " + ( (f(n))/(f(n-r)) );
}
function C(n,r){
return "<strong>Combinations:</strong> " + ( (f(n))/((f(n-r))*(f(r))) );
}
function calc(){
y = document.getElementById('p').checked.valueOf();
z = document.getElementById('c').checked.valueOf();
n = document.getElementById('n').value;
r = document.getElementById('r').value;
if(y==true){ ans = P(n,r) };
if(z==true){ ans = C(n,r) };
document.getElementById('answer').innerHTML= ans;
}