I have a math website http://finitehelp.com that teaches students Finite Math. I thought it would be cool to include a calculator so I made one for combinations and permutations in Javascript. Live calculator is at http://finitehelp.com/finite-calculator.html. I know next to nothing about Javascript and would venture to guess there is a much more efficient way to write the following particularly because of the excessive use of variables. If someone could please help me I would be very grateful.
<script type="text/javascript">
// calculate n!
Math.factorial = function(n)
{
if(typeof n == string ) n = Number(n);
if(typeof n != number || isNaN(n))
{
alert("Factorial requires a numeric argument.");
return null;
}
if (n < 2) return 1;
return (n * Math.factorial(n-1));
}
Math.divide = function(a,b)
{
return a/b;
}
</script>
<form class="form" name="combination" action="">
<p>C(<input type="text" value="n" name="T1" size="1">,<input type="text" value="r" name="T2" size="1">)
<input type="button" value="Calculate"
onclick="var n = T1.value; var r = T2.value; var n_minus_r = parseFloat(n) - parseFloat(r); var numerator = Math.factorial(T1.value); var n_minus_r_fact = Math.factorial(n_minus_r); var r_fact = Math.factorial(r); var denominator = n_minus_r_fact * r_fact; T3.value = Math.divide(numerator,denominator); return true;">
= <input type="text" name="T3" size="12" readonly></p>
</form>