在这里,在加载结果并尝试输入下一个输入后,它只会附加结果,但应清除显示以进行第二次计算。任何帮助将不胜感激。
var arr=[];
var input=0;
function numberBtn(arg){
if ( document.getElementById("currentValue").innerHTML ==="ERROR" || (document.getElementById("currentValue").innerHTML == "0"))
{
document.getElementById("currentValue").innerHTML = "";
}
input += arg;
document.getElementById("currentValue").innerHTML += arg;
}
function calculate(val){
if(input !== 0){
addToArr(input);
}
var answer = val[0];
var divideZero =0;
var i=2
switch(arr[i-1]){
case '+':
answer= Number(val[i-2]) + Number(val[i]);
break;
case '-':
answer-= val[i];
break;
case '/':
if (val[i] == 0) divideZero = 1;
else answer = answer / val[i];
break;
case'*':
//console.log(value,'before multiply');
answer = answer * val[i];
// console.log(val[i],'after multiply');
break;
case'%':
if (val[i] == 0) divideZero = 1;
else{
var q= val[i-2]/val[i];
answer= val[i-2] - (val[i] * Math.floor(q));
}
break;
case'+/-':
answer= -answer;
break;
}
if ( divideZero === 1) {
clearAll();
document.getElementById("currentValue").innerHTML = "ERROR";
}
else
{
document.getElementById("currentValue").innerHTML = answer;
input = answer;
arr = [];
}
}
function addToArr(input){
arr.push(input);
}
function clearAll() {
arr = [];
input = 0;
document.getElementById("currentValue").innerHTML = "0";
}
function operatorBtn(arg){
if (input !== 0) {
addToArr(input);
addToArr(arg);
document.getElementById("currentValue").innerHTML +=arg;
input = 0;
}
}