Как я могу сделать объединенные операции в текстовом вводе без eval? - PullRequest
0 голосов
/ 09 ноября 2019

Я пытаюсь сделать свой собственный калькулятор, проблема в том, что я хотел бы рассчитывать различные операции одновременно, как eval делает

Я получил, чтобы сделать вычисления с 2 числами, но если я введу третьеОперация это не работает, я попытался создать цикл, который делает разделение каждый раз, когда он находит оператор (+, -, *, /), но он не работает ... Я был бы признателен за некоторую помощь, как я уже упоминал ранееЯ кодирую уже около 6 недель.

Кстати, функция prueba на данный момент ничего не делает, это всего лишь несколько снимков, которые я пробовал ....

Спасибо!

function takeValue(x) {
  document.getElementById('screen').value += x;
}

function clearInput() {
  document.getElementById('screen').value = "";
}

function result() {
  let operators = ['+', '-', '*', '/'];
  for (op of operators) {
    checkOperation(op);
  }
}

function checkOperation(oper) {
  let res;
  let actual = document.getElementById('screen').value;
  prueba();
  if (actual.indexOf(oper) != -1) {
    let arr = actual.split(oper, 2);
    switch (oper) {
      case "+":
        res = parseInt(arr[0]) + parseInt(arr[1]);
        break;
      case "-":
        res = parseInt(arr[0]) - parseInt(arr[1]);
        break;
      case "*":
        res = parseInt(arr[0]) * parseInt(arr[1]);
        break;
      case "/":
        res = parseInt(arr[0]) + parseInt(arr[1]);
        break;
    }
    document.getElementById("screen").value = res;
  }
}

function prueba() {
  let actual = document.getElementById('screen').value;
  let temp = [];
  let arrNums = [];
  let arrSigns = [];
  for (i = 0; i < actual.length; i++) {
    temp[i] = actual[i];
  }
  for (tem of temp) {
    if (isNaN(tem)) {
      arrSigns.push(tem);
    } else {
      arrNums.push(tem);
    }
  }
  for (let i = 0; i < arrNums; i++) {
    for (let j = 0; j < arrSigns; j++) {
      if (true) {

      }
    }
  }
}
<html lang="en">

<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" href="style.css">
  <title>Calculator</title>
</head>

<body>
  <div class="mycalc">
    <form name="calculator" id="calculator">
      <h2 class="casio">CASIO</h2>
      <input type="text" name="screen" id="screen">
      <div>
        <input type="button" id="one" value="1" onclick="takeValue(1)">
        <input type="button" id="two" value="2" onclick="takeValue(2)">
        <input type="button" id="three" value="3" onclick="takeValue(3)">
        <input type="button" id="add" value="+" onclick="takeValue('+')">
      </div>
      <div>
        <input type="button" id="four" value="4" onclick="takeValue(4)">
        <input type="button" id="five" value="5" onclick="takeValue(5)">
        <input type="button" id="six" value="6" onclick="takeValue(6)">
        <input type="button" id="sus" value="-" onclick="takeValue('-')">
      </div>
      <div>
        <input type="button" id="seven" value="7" onclick="takeValue(7)">
        <input type="button" id="eight" value="8" onclick="takeValue(8)">
        <input type="button" id="nine" value="9" onclick="takeValue(9)">
        <input type="button" id="mul" value="*" onclick="takeValue('*')">
      </div>
      <div>
        <input type="button" id="zero" value="0" onclick="takeValue(0)">
        <button type="button" id="clear" value="" onclick="clearInput()">AC</button>
        <input type="button" id="zero" value="=" onclick="result()">
        <input type="button" id="div" value="/" onclick="takeValue('/')">


      </div>
    </form>
  </div>
</body>

</html>
...