javascript - Split string into 2 parts with caret -


i have input field wherein user can type expression represent power like: 2^5.

i using following code attempt split string caret , evaluate power math.pow():

var display = $('#disp').val(); var values = display.split('^'); console.log(values);  var base = values[0]; var exponent = values[1];  var powercalc = math.pow(values[0],values[1]); 

however console returns this:

array [ "2", "" ] 

and expression, regardless of base, evaluates 1 (which makes sense). said, how can alter code store second value in array via split()?

note: have tried limiting number of splits 2 , console returned same thing.

i should note also, using button add caret (if makes difference) via function:

function addchar(input, character) {   if (input.value == null || input.value == "0"){     input.value = character}   else{     input.value += character} };  $('#button-power').click(function(){     addchar(this.form.display, '^'); }); 

and display result on click:

$('#button-enter').click(function(){     $('#disp').val(powercalc);  }); 

the script form contains function well:

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 != "%" && ch != "^") {       $('#disp').val("error");         return false       }     }   }   return true } 

i have added small console too. works me, way:

$(function () {    var console = {      log: function (v) {        $("#console").append('console: ' + v + "\n");        window.console.log(v);      }    }    $("#sub").click(function () {      var display = $('#disp').val();      var values = display.split('^');      console.log(values);        var base = values[0];      var exponent = values[1];        var powercalc = math.pow(values[0],values[1]);    });  });
* {font-family: segoe ui;}  #console {position: fixed; bottom: 0; left: 0; right: 0; height: 100px; overflow: auto; font-family: consolas; background: #ccc; border-top: 1px solid #999; margin: 0; font-size: 10pt;}  #console strong:first-child {background: #eee; border-bottom: 1px solid #999; display: block; padding: 3px;}
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>  <input type="text" id="disp"><input type="submit" value="calc" id="sub" />  <pre id="console"><strong>console:</strong></pre>


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -