From f65bbf422ddc798b9d1ebdef0b5d06c5e71ffdb1 Mon Sep 17 00:00:00 2001 From: zalbright90 <61331268+zalbright90@users.noreply.github.com> Date: Tue, 16 Jul 2024 00:24:19 -0700 Subject: [PATCH] Refactor all the querySelectors to one function --- main.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/main.js b/main.js index 1c36209..78e876e 100644 --- a/main.js +++ b/main.js @@ -113,8 +113,15 @@ document.addEventListener('DOMContentLoaded', function() { } } + function bindButtonEvents(className, callback) { + const button = document.querySelector(`.${className}`); + if (button) { + button.addEventListener('click', callback); + } + } + ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'].forEach((className, index) => { - document.querySelector(`.${className}`).addEventListener('click', () => updateDisplay(index.toString())); + bindButtonEvents (className, () => updateDisplay(index.toString())); }); [ @@ -123,28 +130,21 @@ document.addEventListener('DOMContentLoaded', function() { { op: '*', className: 'multiply' }, { op: '/', className: 'divide' } ].forEach(({ op, className }) => { - const operatorChoice = document.querySelector(`.${className}`); - operatorChoice.addEventListener('click', () => setOperator(op)); + bindButtonEvents(className, () => setOperator(op)); }); - const dotButton = document.querySelector('.dot') - dotButton.addEventListener('click', () => { - if (!displayValue.includes('.')) { + bindButtonEvents('dot', () => { + if(!displayValue.includes('.')) { updateDisplay('.'); } }); - document.querySelector('.clear').addEventListener('click', clearDisplay); - document.querySelector('.toggle-sign').addEventListener('click', toggleSign); - document.querySelector('.backspace').addEventListener('click', backspace); - document.querySelector('.percent').addEventListener('click', percent); + bindButtonEvents('clear', clearDisplay); + bindButtonEvents('toggle-sign', toggleSign); + bindButtonEvents('backspace', backspace); + bindButtonEvents('percent', percent); - const equalsButton = document.querySelector('.equals'); - if (equalsButton) { - equalsButton.addEventListener('click', () => { - calculate(); - }); - }; + bindButtonEvents('equals', calculate); const numpadKeyMap = { 'Numpad0': '0',