用插入符号将字符串拆分为2个部分

时间:2021-01-14 21:41:15

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

我有一个输入字段,其中用户可以键入表达式来表示如下的幂: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():

我使用以下代码尝试使用插入符分割此字符串,然后使用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()?

并且表达式,无论基数如何,总是计算为1(这是有道理的)。也就是说,如何通过split()更改此代码以将第二个值存储在此数组中?

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

注意:我已经尝试将分割数量限制为2,并且控制台返回相同的内容。

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
}

2 个解决方案

#1


1  

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>

#2


0  

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>

#1


1  

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>

#2


0  

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>