Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

331
Visualizações
why is my code saying' Uncaught SyntaxError: Identifier 'calculator' has already been declared' in the console?

I tried making a calculator, but checking it at this stage, it's displaying some syntax error.

Please I need help. How can I correct this? There is a class of calculator there and I represented it with the 'new' word. This is vanilla JavaScript. Tried calling the function, but it kept on giving me the error in my console. Please could someone help me?

class calculator {
  constructor(previousOperandTextElement, currentOperandTextElement) {
    this.perviousOperandTextElement = previousOperandTextElement
    this.currentOperandTextElement = currentOperandTextElement
    this.clear()
  }
    
  clear() {
    this.currentOperand = ''
    this.previousOperand = ''
    this.operation = undefined
  }
    
  delete() {
  }

  appendNumber(number) {
    this.currentOperand = number
  }
    
  chooseOperation(operation) {   
  }
    
  compute() {
  }

  updateDisplay() {
    this.currentOperandTextElement.innerText = this.currentOperand;
  }
}
    
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()
  })
})
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Identifier 'calculator' has already been declared' means you've used name 'calculator' twice, in your code - class is named 'calculator' and constant for calculator object is also 'calculator'.

Change class name to 'Calculator':

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

  clear() {
    this.currentOperand = ''
    this.previousOperand = ''
    his.operation = undefined
  }

  delete() {

  }

  appendNumber(number) {
   this.currentOperand = number
  }

  chooseOperation(operation) {

  }

  compute() {

  }

  updateDisplay() {
    this.currentOperandTextElement.innerText = this.currentOperand;
  }
}



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]')
    
// error was here
const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement)



numberButtons.forEach(button => {
  button.addEventListener('click', () => {
    calculator.appendNumber(button.innerText)
    calculator.updateDisplay()
  })
})

P.S. always name your classes with upper case first letter, that is the convention

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda