有一个数组,其中记录了计算器上的所有点击。进一步,这个数组在页面上显示为字符串,即=5+5=10可以显示在历史中(第一个乘号,不应该是,连续的符号,也不应该是)。如何进行例外处理,以便并非所有内容都被推入数组?
const calculator = {
displayValue: '0',
firstOperand: null,
waitingForSecondOperand: false,
operator: null,
expressionData: [],
historyData: [],
};
function inputDigit(digit) {
const {
displayValue,
waitingForSecondOperand
} = calculator;
if (waitingForSecondOperand === true) {
calculator.displayValue = digit;
calculator.waitingForSecondOperand = false;
} else {
calculator.displayValue = displayValue === '0' ? digit : displayValue + digit;
}
calculator.expressionData.push(digit);
}
function inputDecimal(dot) {
// If the `displayValue` does not contain a decimal point
if (!calculator.displayValue.includes(dot)) {
// Append the decimal point
calculator.displayValue += dot;
calculator.expressionData.push(dot);
}
}
function handleOperator(nextOperator) {
calculator.expressionData.push(nextOperator);
const {
firstOperand,
displayValue,
operator
} = calculator
const inputValue = parseFloat(displayValue);
if (operator && calculator.waitingForSecondOperand) {
calculator.operator = nextOperator;
return;
}
if (firstOperand == null) {
calculator.firstOperand = inputValue;
} else if (operator) {
const currentValue = firstOperand || 0;
const result = performCalculation[operator](currentValue, inputValue);
calculator.displayValue = String(result);
calculator.firstOperand = result;
calculator.expressionData.push(result);
calculator.historyData.push({expression: calculator.expressionData.join('')});
calculator.expressionData = [];
resultation();
}
calculator.waitingForSecondOperand = true;
calculator.operator = nextOperator;
}
function resultation () {
let resulstHistory = document.getElementById('results');
let string = '';
for (let key in calculator.historyData) {
string += '' + calculator.historyData[key]['expression'] + "<br>";
console.log(calculator.historyData[key]['expression'])
};
resulstHistory.innerHTML = string
}
const performCalculation = {
'/': (firstOperand, secondOperand) => firstOperand / secondOperand,
'*': (firstOperand, secondOperand) => firstOperand * secondOperand,
'+': (firstOperand, secondOperand) => firstOperand + secondOperand,
'-': (firstOperand, secondOperand) => firstOperand - secondOperand,
'=': (firstOperand, secondOperand) => secondOperand
};
function resetCalculator() {
calculator.displayValue = '0';
calculator.firstOperand = null;
calculator.waitingForSecondOperand = false;
calculator.operator = null;
}
function updateDisplay() {
const display = document.querySelector('.calculator-screen');
display.value = calculator.displayValue;
}
updateDisplay();
const keys = document.querySelector('.calculator-keys');
keys.addEventListener('click', (event) => {
const {
target
} = event;
if (!target.matches('button')) {
return;
}
if (target.classList.contains('operator')) {
handleOperator(target.value);
updateDisplay();
return;
}
if (target.classList.contains('decimal')) {
inputDecimal(target.value);
updateDisplay();
return;
}
if (target.classList.contains('all-clear')) {
resetCalculator();
updateDisplay();
return;
}
inputDigit(target.value);
updateDisplay();
});
如果我想以这种方式解决它,我会告诉你我自己会使用的算法(这不是最终的 100% 算法适用于所有情况,但很容易补充它以获得所需的结果):
canBeFirst) 来判断输入字符是否是可以先出现的字符isSpecSymbol(addToArr) 来查看数组的最后一个元素和输入字符。如果数组为空,则调用canBeFirst,如果可能,则添加它,如果不是,则不添加。如果数组不为空,那么我们调用isSpecSymbol数组的输入和最后一个元素,如果两者都是true,那么我们不会添加,如果一个true是,另一个false,那么我们添加我希望我能帮上忙