简体   繁体   English

如何让我的计算器在按下操作员按钮后计算?

[英]How can I make my calculator evaluate after an operator button is pressed?

Im trying to make a calculator however i cant figure out how to make it chain operations like 4+4+4-5.我正在尝试制作一个计算器,但我无法弄清楚如何使它像 4+4+4-5 这样的链式运算。 instead i have to do equals button between each of them(so 4+4 = 8, i then have to press + 4 then = to get a total instead of it doing it after + is pushed. i'd like it to instead calculate when the operator button is pressed, get an answer in that situation.相反,我必须在他们每个人之间做等于按钮(所以 4+4 = 8,然后我必须按 + 4 然后 = 来获得总数,而不是在 + 被按下后这样做。我希望它改为计算当按下操作员按钮时,在那种情况下得到答案。

Here is my javascript. https://codepen.io/hbz19/pen/KKBgYMP这是我的javascript。https://codepen.io/hbz19/pen/KKBgYMP

function add(x, y) {
        return x + y
    };

    function subtract(x, y) {
         return x - y
};

function multiply(x, y) {
    return x * y
};

function divide(x, y) {
    return x / y
};

function multiplyarray(array2) {
  return array2.reduce((a, b)=> a*b)
};

function operate(firstNum, secondNum, operatorChoice) {
 let firstNumToNumber = parseInt(firstNum);
 let secondNumToNumber = parseInt(secondNum);
 if (operatorChoice === "+") {
   let answer = add(firstNumToNumber, secondNumToNumber);
   console.log(answer);
   displayArea.innerHTML = answer;
   

} if (operatorChoice === "-") {
    answer = subtract(firstNumToNumber, secondNumToNumber);
    console.log(answer);
    displayValue = answer;

  } else if (operatorChoice === "/") {
    answer = divide(firstNumToNumber, secondNumToNumber);
    console.log(answer);
    displayArea.innerHTML = answer;

  } else if (operatorChoice === "*") {
    answer = multiply(firstNumToNumber, secondNumToNumber);
    console.log(answer);
    displayArea.innerHTML = answer;

  }
  
};
  let answer = ""
  let value = "";
  let firstNum = "";
  let secondNum = "";
  let operatorChoice = "";
  let currentValue = "";
  let previousValue = "";
  let displayValue = "";
  let displayArea = document.getElementById("display");
  displayArea.innerHTML += displayValue;
 
  let buttons = document.querySelectorAll(".numbers");
  buttons.forEach(button => {
    button.addEventListener("click", (e) => {
     
    
      updateDisplay(e);
    })
  });


  let updateDisplay = (e) => {
if(displayArea.innerHTML == answer) displayArea.innerHTML = ""
    currentValue = e.target.innerHTML;
    previousValue = displayArea.innerHTML;
    displayValue = previousValue + currentValue;
    displayArea.innerHTML = displayValue
  }


let operateButtons = document.querySelectorAll(".operator");
operateButtons.forEach(button => {
  button.addEventListener("click", (e) => {
   
    startCalculation(e);
  })
});

let startCalculation = (e) => {
firstNum = displayArea.innerHTML;
operatorChoice = e.target.innerHTML;
console.log(firstNum);
console.log(operatorChoice);
displayArea.innerHTML = "";
};

let equalButton = document.querySelectorAll(".equal");
equalButton.forEach(button => {
  button.addEventListener("click", (e) => {
   
    ;
    calculate(e);
  })
});

let calculate = (e) => {
  secondNum = displayValue;
  console.log(firstNum, operatorChoice, secondNum);
  operate(firstNum, secondNum, operatorChoice);
}

let clearButton = document.querySelectorAll(".reset");
clearButton.forEach(button => {
  button.addEventListener("click", (e) => {

    clear(e);
  })
});

function clear (e) {
  let firstNum = "";
  let secondNum = "";
  let operatorChoice = "";
  let currentValue = "";
  let previousValue = "";
  let displayValue = "";
  displayArea.innerHTML = "";
  console.log(displayValue, firstNum, secondNum,operatorChoice, currentValue, previousValue)
  return displayValue, firstNum, secondNum, operatorChoice, currentValue, previousValue
}````

This is the expected behavior since your functions take 2 numbers.这是预期的行为,因为您的函数需要 2 个数字。 Take operate for example以操作为例

function operate(firstNum, secondNum, operatorChoice) {
 let firstNumToNumber = parseInt(firstNum);
 let secondNumToNumber = parseInt(secondNum);
//...
}

The logic you use is: look at operator, based on operator calculate number one with number two您使用的逻辑是:查看运算符,基于运算符计算一号和二号

What you need to do is to be able to take more numbers and operators and calculate them, respecting the order of operations, when equal is pressed您需要做的是能够获取更多数字和运算符并计算它们,并在按下相等时遵守运算顺序

There are different ways to go about that, fortunately js has a handy function called eval() that can correctly calculate a string.关于 go 有不同的方法,幸运的是 js 有一个方便的 function 称为eval()可以正确计算字符串。 one easy way is to store your numbers and operators in an array using push everytime a number or an operator is clicked一种简单的方法是每次单击数字或运算符时使用 push 将您的数字和运算符存储在数组中

You'll end up with something like你最终会得到类似的东西

let numbersToCalculate = [45, '+', 3, '*', 10]

Then we join the array to end up with a string然后我们加入数组以一个字符串结束

const numbersStr = numbersToCalculate.join(' ')

we end up with我们最终得到

"45 + 3 * 10"

Then, when equal is pressed we can do然后,当按下等于时,我们可以做

eval(numbersStr)

and get the result并得到结果

You could also directly push to a string, I personally like to use an array first.也可以直接push到字符串,我个人喜欢先用数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript Calculator:如何根据条件创建操作员按钮? - Javascript Calculator: How can I create an operator button based on a condition? 如何正确评估if语句? - How can I make my if statement evaluate correctly? 如何仅在按下一个键后运行我的 function? - How can I run my function only after a key is pressed? 按下按钮时如何模拟按下的不同按钮? - How can I simulate a different button pressed when a button is pressed? 我该怎么做才能让我在计算器应用程序中按下的每个按钮都将数字组合在一起? - How do I make it so each button I press in my calculator app combines the numbers together? 我无法使用 JavaScript 在平方计算器中使用 addEventListener 按下的键进行 console.log 记录? - I can't console.log the key that I pressed with addEventListener in my squaring calculator using JavaScript? 我按下按钮一次后如何使图像切换回原始图像 src - How to I make the image switch back to the original image src after Iv'e pressed the button once 如何确保用户只按一下我的喜欢按钮? - How do I make sure my like button is pressed only once by user? Javascript - 帮助更新我的计算器的显示以显示按下按钮的数量 - Javascript - Help to update the display of my calculator to display the number of the button pressed 如何在按下按钮(动画)后使页面左右移动? - How to make pages to move left or right after button was pressed(animation)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM