Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

332
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!