javascript - Getting one input field to take two entered values and perform Math.pow() -
i have javascript / jquery calculator button performs mathematical operation of raising first entered integer second entered integer via function:
$('#button-power').click(function(form){ var base = this.form.display.value; // 1st value entered user settimeout (function(){ $("#disp").val('0'); // resets calc display 0 accept second argument },0.5); $("#button-enter").click(function(form){ // 'equals' button var exponent = this.form.display.value; // 2nd value entered user var result = math.pow(base,exponent); settimeout (function (form){ $('#disp').val(result); },1); }); }); where #disp calculator display. function works fine , produces correct answer, causes other buttons on calculator behave incorrectly after calculation performed using power button. know has fact function requires 2 values other calculator buttons require 2 inputs (one performs permutation calculations, example) exhibit similar behavior. trying divide 8 2 after performing power function return off wall 35 instance.
relevant functions may contributing behavior include:
function checknum(str) { (var = 0; < str.length; i++) { var ch = str.substring(i, + 1) if (ch < "0" || ch > "9") { if (ch != "/" && ch != "*" && ch != "+" && ch != "-" && ch != "." && ch != "(" && ch != ")" && ch != "%") { $('#disp').val("error"); return false } } } return true } function addchar(input, character) { if (input.value == null || input.value == "0"){ input.value = character} else{ input.value += character} }; $.each([1, 2, 3, 4, 5, 6, 7, 8, 9, 0], function(i, e) { $('#button-' + e).click(function() { if ($('#disp').hasclass("result")) { $('#disp').removeclass("result").val(""); } addchar(this.form.display, e); }); }); other buttons on calculator require 1 input, cosine button example, utilize javascript , jquery in way:
function cos(form) { form.display.value = math.cos(form.display.value); } $('#button-cos').click(function() { if (checknum(this.form.display.value)) { cos(this.form); $('#disp').addclass("result"); } }); i wondering if possible calculator display 2^5 , parse somehow perform math.pow(2,5)? think solve problem.
here's fiddle of calculator: https://jsfiddle.net/lh2x7vja/819/
apologies messy script - have yet clean up!
Comments
Post a Comment