[英]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.