snack-boomz / GUI-Calculator

Quick GUI Calculator - The Odin Project Web Development Online Curriculum: Foundations Course: GUI Calculator
https://snack-boomz.github.io/GUI-Calculator/
1 stars 0 forks source link

Use a loop for the button click listeners #6

Open oparkins opened 3 years ago

oparkins commented 3 years ago

I would have recommended using a loop to add all of those event listeners. It would be something like this (untested:

const buttons = [btnOne, btnTwo, btnThree, ...];

for (const [index, button] of buttons.entries()) {
    button.addEventListener('click', () => {
        calculatorDisplayContent.textContent += index.toString();
        calculatorDisplay.appendChild(calculatorDisplayContent);
        checkIfEvaluationIsSolvable();
        phaseInAllExpressions();
        checkForOverflow();
    });
}

That should make the code more readable and maintainable.

snack-boomz commented 3 years ago

So this is something I knew in my head from basic principles I've learned in the past (DRY... don't repeat yourself); but I hadn't figured out a way to implement it in code yet. Looks like this is exactly what I needed to figure this out!

Thanks!