if语句中的Eval()

时间:2018-03-09 00:30:37

标签: javascript html electron eval calculator

HTML& JS

<!DOCTYPE html>
<html>
  <head>
    <title>Calculator</title>
    <!-- Allows me to use jQuery -->
    <script>
    if (typeof module === 'object') {window.module = module; module = undefined;}
    </script>
    <script src="assets/libraries/jq.js"></script>
    <link rel="stylesheet" href="assets/css/style.css">
    <!-- End of "Allows me to use jQuery" -->
    <script>
    if (window.module) module = window.module;
  </script>
  </head>
  <body>
    <!-- "vysledek" means in English "result" -->
    <input id="vysledek" type="text" autofocus>
        <br>
                  <!-- "kalkulacka" means in my language "calculator" -->
      <button class="C" onClick="kalkulacka('C')">C</button>
      <button onClick="kalkulacka('(')">(</button>
      <button onClick="kalkulacka(')')">)</button>
      <button onClick="kalkulacka('/')">&#247;</button>
        <br>
      <button onClick="kalkulacka(7)">7</button>
      <button onClick="kalkulacka(8)">8</button>
      <button onClick="kalkulacka(9)">9</button>
      <button onClick="kalkulacka('*')">&#215;</button>
        <br>
      <button onClick="kalkulacka(4)">4</button>
      <button onClick="kalkulacka(5)">5</button>
      <button onClick="kalkulacka(6)">6</button>
      <button onClick="kalkulacka('-')">&#8722;</button>
        <br>
      <button onClick="kalkulacka(1)">1</button>
      <button onClick="kalkulacka(2)">2</button>
      <button onClick="kalkulacka(3)">3</button>
      <button onClick="kalkulacka('+')">&#43;</button>
        <br>
      <button onClick="kalkulacka('.')">,</button>
      <button onClick="kalkulacka(0)">0</button>
      <button onClick="kalkulacka('=')">=</button>
      <button onClick="kalkulacka('⌫')">⌫</button>
  </body>
</html>


<script>
// "boxik" here just represents that input field
const boxik = document.getElementById("vysledek");

function kalkulacka(x) {
  if (x == "C") {
    //This clears whole input
    boxik.value = "";
  } else if (x == "⌫") {
    //This removes last digit
    boxik.value = boxik.value.substring(0, boxik.value.length-1);
  } else if (x == "=") {
    //Here is our problem, I expect this to check if it was calculated or
    //not      
    if (eval(boxik.value)) {
      boxik.value = eval(boxik.value);
    } else {
      $("#vysledek").css("border-bottom", "2px solid #bd1515");
    }
  }
  //This basically adds all digits, + etc. together
  else {
    boxik.value += x;
  }
}
</script>

这是JSFiddle的工作:https://jsfiddle.net/mg7zmLos/7/

所以基本上今天我开始使用Electron并且我正在研究计算器应用程序,我刚刚编写了硬编码,但它可以工作(到目前为止尚未完成),但我想检查一下我的计算是否良好,如果是,那么我只想做boxik.value = eval(boxik.value)如果它是螺丝,然后使用jQuery $("#vysledek").css("border-bottom", "2px solid #bd1515")使我的边框变红,但我不知道如何检查它是否拧紧......

1 个答案:

答案 0 :(得分:0)

摆脱if,而不是像eval这样试试/捕捉:

    try {
      boxik.value = eval(boxik.value);
      $("#vysledek").css("border-bottom", "2px solid #5faed0");
    }
    catch (e) {
      $("#vysledek").css("border-bottom", "2px solid #bd1515");
    }