User:KCCreations/MultipurposeCalculator.js

//Nifty little calculator //Uses modified code written by User:Dragonfree97 //Can-do: Subtraction, multiplication, division //To-do: Fix addition function

function numberWithCommas(x) { return x.toString.replace(/\B(?=(\d{3})+(?!\d))/g, ","); } function addition { addend1 = $("#addend1").val; addend2 = $("#addend2").val; sum = $("#sum").val; sum = addend1 + addend2; $("#addend1").html(numberWithCommas(addend1)); $("#addend2").html(numberWithCommas(addend2)); $("#sum").html(numberWithCommas(sum)); }

function subtraction { minuend = $("#minuend").val; subtrahend = $("#subtrahend").val; difference = $("#difference").val; difference = minuend - subtrahend; $("#minuend").html(numberWithCommas(minuend)); $("#subtrahend").html(numberWithCommas(subtrahend)); $("#difference").html(numberWithCommas(difference)); }

function multiplication { multiplicand = $("#multiplicand").val; multiplier = $("#multiplier").val; product = $("#product").val; product = multiplicand * multiplier; $("#multiplicand").html(numberWithCommas(multiplicand)); $("#multiplier").html(numberWithCommas(multiplier)); $("#product").html(numberWithCommas(product)); }

function division { dividend = $("#dividend").val; divisor = $("#divisor").val; quotient = $("#quotient").val; quotient = dividend / divisor; $("#dividend").html(numberWithCommas(dividend)); $("#divisor").html(numberWithCommas(divisor)); $("#quotient").html(numberWithCommas(quotient)); }

function calculator { $.showCustomModal( 'Calculator', '         Add!     Total: 0             Subtract!     Total: 0              <td colspan="2" style="text-align:center; padding: 5px;"><button id="multiplication-button" onClick="multiplication;">Multiply! Total: 0   <td style="text-align: right;"><input style="width: 100px;" type="number" value="0" id="dividend">      <td style="text-align: right;"><input style="width: 100px;" type="number" value="0" id="divisor">   <td colspan="2" style="text-align:center; padding: 5px;"><button id="division-button" onClick="division;">Divide! Total: 0  '); }

$('.toolbar .tools').append($('<a href="javascript:calculator">Calculator</a></li>'));