Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project 5, Calculator. Refactoring: nested if else statements #1095

Open
ipzk241-zhdv opened this issue Jan 28, 2025 · 0 comments · May be fixed by #1096
Open

Project 5, Calculator. Refactoring: nested if else statements #1095

ipzk241-zhdv opened this issue Jan 28, 2025 · 0 comments · May be fixed by #1096
Labels
duplicate This issue or pull request already exists

Comments

@ipzk241-zhdv
Copy link

Duplicate code.
The multiple else if checks on button.classList.contains() are repetitive, and this pattern can be refactored using a mapping approach. This would make code more maintainable and extensible.

Source file: https://github.com/ZeroOctave/ZeroOctave-Javascript-Projects/blob/main/assets/Js/Calculator.js
Code fragment:

operationButtons.forEach(button => {
  button.addEventListener('click', () => {
    if (button.classList.contains("special")) {
      if (button.classList.contains("pie-button")) {
        calculator.specialOperation(1)
      }
      else if (button.classList.contains("e-button")) {
        calculator.specialOperation(2)
      }
      else if (button.classList.contains("square")) {
        calculator.specialOperation(3)
      }
      else if (button.classList.contains("pow")) {
        calculator.specialOperation(4)
      }
      else if (button.classList.contains("squareRoot")) {
        calculator.specialOperation(5)
      }
      else if (button.classList.contains("logBase10")) {
        calculator.specialOperation(6)
      }
    }
    else calculator.chooseOperation(button.innerText)
    calculator.updateDisplay()
  })
})
@github-actions github-actions bot added the duplicate This issue or pull request already exists label Jan 28, 2025
@ipzk241-zhdv ipzk241-zhdv linked a pull request Jan 28, 2025 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant