问题描述:

I have an input field wherein a user can type an expression to represent a power like: 2^5.

I am using the following code to attempt to split this string with the caret and then evaluate the power with 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 the console returns this:

Array [ "2", "" ]

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

Note: I have tried limiting the number of splits to 2 and the console returned the same thing.

I should note also, that I am using a button to add the caret (if that makes a difference) via this 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 this to display the result on click:

$('#button-enter').click(function(){

$('#disp').val(powerCalc);

});

The script for the form contains this function as well:

function checkNum(str) {

for (var i = 0; i < str.length; i++) {

var ch = str.substring(i, 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 a small console too. This works for me, this 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>
网友答案:

I don't see the problem. Looks like your code is working just fine.

$('#calc').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]);
  $('#out').text(powerCalc);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="disp" type="text" /><button id="calc">Pow!</button>&nbsp;=&nbsp;<span id="out"></span>
相关阅读:
Top