See the full calculator with HTML -> https://gist.github.com/1861120 You should be able to drag that html file from github into a browser and start using it. I'm only putting the js below. The calculator works fine but JS Lint is telling me I have an unexpected 'function' doCalc line 9 character 1.
Also just looking for some general tips on best practices and how this might be refactored. Any help is appreciated. Thank you.
var type, n, r, formula;
Math.factorial = function (n) {
if (n < 2) {
return 1;
}
return (n * Math.factorial(n - 1));
}
function doCalc(type) {
switch (type) {
case 'permutation':
n = document.permutation.T1.value;
r = document.permutation.T2.value;
formula = function() {
document.permutation.T3.value = Math.factorial(n) / Math.factorial(n - r);
}
break;
case 'combination':
n = document.combination.T1.value;
r = document.combination.T2.value;
formula = function() {
document.combination.T3.value = Math.factorial(n) / (Math.factorial(r) * Math.factorial(n - r));
}
break;
}
n = Number(n);
r = Number(r);
if (isNaN(n) || isNaN(r)) {
alert("Factorial requires a numberic argument.");
return null;
}
formula();
}
window.onload = function() {
document.getElementById('calculate-p').onclick = function() {
doCalc('permutation');
}
document.getElementById('calculate-c').onclick = function() {
doCalc('combination');
}
}