function costOf(score) { if(score <= 8) return 0; if(score <= 13) return score - 8; if(score == 14) return 7; if(score == 15) return 9; if(score == 16) return 11; if(score == 17) return 14; if(score == 18) return 18; throw new OutOfRangeException(); } function calcMod(score) { return Math.floor(0.5 * (score-10)); } function getMod(score) { var mod = calcMod(score); if(mod >= 0) return "+" + mod; return mod; } function recalcTotal() { var sum = 0; $("#str,#dex,#con,#int,#wis,#cha").each(function() { var v = $(this)[0]; var score = parseInt(v.value); sum += costOf(score); $("#" + v.id + "mod").empty().append(getMod(score)); }); $("#total").empty().append(sum-10); } function resetScores() { $("#str,#dex,#con,#int,#wis").each(function() { $(this)[0].value = 10; }); $("#cha").each(function() { $(this)[0].value = 8; }); recalcTotal(); } $(document).ready(function() { $("#abscores input").bind("change", recalcTotal); resetScores(); });