javascript - 如何在我的计算器中存储结果和查看以前的计算结果?

标签 javascript html css calculator

我正在尝试创建一个计算器来存储我的结果以查看以前的计算。我正在努力学习并获得实习职位。任何帮助将不胜感激!计算器目前工作正常,我的想法是要么有一个单独的部分来记录历史,要么修改计算器的顶行以显示之前的计算。我一直在学习 youtube 教程,这就是我努力添加额外内容的原因。

class Calculator {
constructor(previousOperandTextElement, currentOperandTextElement) {
    this.previousOperandTextElement = previousOperandTextElement;
    this.currentOperandTextElement = currentOperandTextElement;
    this.clear();
}

//clear all, default to empty string
clear() {
    this.currentOperand = '';
    this.previousOperand = '';
    this.operation = undefined;
}

//delete single number using slice method
delete() { 
    this.currentOperand = this.currentOperand.toString().slice(0,-1);
}

//add number and only allow a single period
appendNumber(number) {
    if (number === '.' && this.currentOperand.includes('.')) return;
    this.currentOperand = this.currentOperand.toString() + number.toString();
}

//if operand is empty return
//sum is carried out if you have 2 operands and placed in previous operand  
//changes operand from current to previous
chooseOperation(operation) {
    if (this.currentOperand === '') return;
    if (this.previousOperand !== '') {
        this.compute();
    }
    this.operation = operation;
    this.previousOperand = this.currentOperand;
    this.currentOperand = '';
}

//calculate 
//dont compute if previous or current operand is empty
//switch statement for which operation is selected
compute() {
    let computation;
    const prev = parseFloat(this.previousOperand);
    const current = parseFloat(this.currentOperand);
    if (isNaN(prev) || isNaN(current)) return;
    switch(this.operation) {
        case '+':
            computation = prev + current;
            break;
        case '-':
            computation = prev - current;
            break;
         case '*':
            computation = prev * current;
            break;
        case '÷':
            computation = prev / current;
            break;
        default:
            return;
    }
    this.currentOperand = computation;
    this.operation = undefined;
    this.previous = '';
}

//update display and handles 
//if operation is not null then show previous operand and operation
//remove previous operand when calculated
updateDisplay() {
    this.currentOperandTextElement.innerText = this.currentOperand;    
    if (this.operation != null) {
        this.previousOperandTextElement.innerText = 
            `${this.previousOperand} ${this.operation}`;
    } else {
        this.previousOperandTextElement.innerText = '';
    }

}
}

//variables
const numberButtons = document.querySelectorAll('[data-number]')
const operationButtons = document.querySelectorAll('[data-operation]')
const equalsButton = document.querySelector('[data-equals]')
const deleteButton = document.querySelector('[data-delete]')
const previousButton = document.querySelector('[data-previous]')
const allClearButton = document.querySelector('[data-all-clear]')
const previousOperandTextElement = document.querySelector('[data-previous-        operand]')
const currentOperandTextElement = document.querySelector('[data-current-   operand]')

//create a new class and pass everything into it
const calculator = new Calculator(previousOperandTextElement,     currentOperandTextElement);

//Loop over all buttons and add event listener
numberButtons.forEach(button => {
button.addEventListener('click', () => {
    calculator.appendNumber(button.innerText);
    calculator.updateDisplay();
})
})

//Loop over operation buttons and add event listener
operationButtons.forEach(button => {
button.addEventListener('click', () => {
    calculator.chooseOperation(button.innerText);
    calculator.updateDisplay();
})
})

//equals button
equalsButton.addEventListener('click', button => {
calculator.compute();
calculator.updateDisplay();
})

//clear button
allClearButton.addEventListener('click', button => {
calculator.clear();
calculator.updateDisplay();
})


//delete button
deleteButton.addEventListener('click', button => {
calculator.delete();
calculator.updateDisplay();
})

//previous button
previousButton.addEventListener('click', button => {

calculator.updateDisplay();
})

最佳答案

您可以使用备忘录设计模式来保留操作日志,它允许您撤消操作:https://www.dofactory.com/javascript/memento-design-pattern .您可以在此处找到应用于计算器的示例:https://dev.to/shikaan/design-patterns-in-web-development---2-memento-253j这样您就可以调整您的代码。

您也可以使用结合命令存储的命令模式来记录操作:https://www.dofactory.com/javascript/command-design-pattern

关于javascript - 如何在我的计算器中存储结果和查看以前的计算结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57351044/

相关文章:

javascript - Webview 重定向问题

javascript - 检测伪类 :invalid from javascript

javascript - 使用 'order' 属性在兄弟之间定位 flex 元素

css - Bootstrap 列表组元素颜色不起作用

javascript - 在 Div 上应用两个悬停效果

javascript - NodeJS 中未找到错误

javascript - 在滚动上添加 class=active

javascript - 如何在 jQuery Mobile 中制作标签栏?

html - 响应式布局问题 div 布局

jquery - 同位素自动高度修复