JavaScript计算器工作不正常,不确定代码的哪部分不正确
我已经使用html和css创建了一个计算器,我选择的用于处理html元素的编程语言是JavaScript,但我的代码似乎都不起作用,我不确定哪里出了问题 所有的按钮都不起作用,我试图做的操作也不起作用,似乎我遗漏了什么 有人能帮我找出代码的问题吗? 非常感谢你JavaScript计算器工作不正常,不确定代码的哪部分不正确,javascript,function,methods,calculator,operation,Javascript,Function,Methods,Calculator,Operation,我已经使用html和css创建了一个计算器,我选择的用于处理html元素的编程语言是JavaScript,但我的代码似乎都不起作用,我不确定哪里出了问题 所有的按钮都不起作用,我试图做的操作也不起作用,似乎我遗漏了什么 有人能帮我找出代码的问题吗? 非常感谢你 class Calculator { constructor(previousOperandTextElement, currentOperandTextElement) { this.previousOp
class Calculator {
constructor(previousOperandTextElement, currentOperandTextElement) {
this.previousOperandTextElement = previousOperandTextElement
this.currentOperandTextElemement = currentOperandTextElement
this.clear()
}
clear() {
this.currentOperand = ''
this.previousOperand = ''
this.operation = undefined
}
delete() {
this.currentOperand = this.currentOperand.toString().slice(0, -1)
}
appendNumber(number) {
if (number === '.' && this.currentOperand.includes('.')) return
this.currentOperand = this.currentOperand.toString() + number.toString()
}
chooseOperation(operation) {
if (this.currentOperand === '.') return
if (this.previousOperand !== '') {
this.compute()
}
this.operation = operation
this.previousOperand = this.currentOperand
this.currentOperand = ''
}
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.previousOperand = ''
}
getDisplayNumber(number) {
const stringNumber = number.toString()
const integerDigits = parseFloat(stringNumber.split('.')[0])
const decimalDigits = stringNumber.split('.')[1]
let integerDisplay
if (isNan(integerDigits)) {
integerDisplay = ''
} else {
integerDisplay integerDigits.toLocalString('en', {
maximumFractionDigits: 0
})
}
if (decimalDigits != null) {
return `${integerDisplay}.${decimalDigits}`
} else {
return integerDisplay
}
}
updateDisplay() {
this.currentOperandTextElemement.innerText =
this.getDisplayNumber(this.currentOperand)
if (this.operation != null) {
this.previousOperandTextElement.innerText =
`${this.getDisplayNumber(previousOperand)} ${this.operation}`
} else {
this.previousOperandTextElement.innerText = ''
}
}
}
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 allClearButton = document.querySelector('[data-all-clear]')
const previousOperandTextElement = document.querySelector('[data-previous-operand]')
const currentOperandTextElement = document.querySelector('[data-current-operand]')
const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement)
numberButtons.forEach(button => {
button.addEventListener('click', () => {
calculator.appendNumber(button.innerText)
calculator.updateDisplay()
})
})
operationButtons.forEach(button => {
button.addEventListener('click', () => {
calculator.chooseOperation(button.innerText)
calculator.updateDisplay()
})
})
equalsButton.addEventListener('click', button => {
calculator.compute()
calculator.updateDisplay()
})
allClearButton.addEventListener('click', button => {
calculator.clear()
calculator.updateDisplay()
})
deleteButton.addEventListener('click', button => {
calculator.delete()
calculator.updateDisplay()
})
假设您开始按12+3.2=-它在控制台中显示什么?完成这些操作后,用控制台的副本/粘贴更新问题。嘿@MadaManu,我成功了,我犯了一些简单的错误,比如,我写了“isNan”作为“isNan”的对立面,我在integerDisplay和integerDigits之间漏掉了一个“=”符号。为了定位,我还忘了在“Locale”中添加一个“e”,基本上是愚蠢的区分大小写的错误,谢谢你的帮助!!假设您开始按12+3.2=-它在控制台中显示什么?完成这些操作后,用控制台的副本/粘贴更新问题。嘿@MadaManu,我成功了,我犯了一些简单的错误,比如,我写了“isNan”作为“isNan”的对立面,我在integerDisplay和integerDigits之间漏掉了一个“=”符号。为了定位,我还忘了在“Locale”中添加一个“e”,基本上是愚蠢的区分大小写的错误,谢谢你的帮助!!