From 36762b9ddf63471b8397857e606aaa3a1733b90b Mon Sep 17 00:00:00 2001 From: Liam Swayne Date: Wed, 20 Mar 2024 10:31:15 -0400 Subject: [PATCH] minor refactor and cleanup --- site/script.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/site/script.js b/site/script.js index 04e2ef1..692db8b 100644 --- a/site/script.js +++ b/site/script.js @@ -11,20 +11,17 @@ function toggleOrganization(organizationId) { slider.disabled = !isChecked; // Reset slider value and update organization quantity slider.value = 0; - updateOrganizationQuantity(organizationId, organizationId + 'Output'); + updateOrganizationQuantity(organizationId); // Recalculate results calculateResults(); } // Update organization quantity output -function updateOrganizationQuantity(sliderId, outputId) { - // Get slider and output elements - const slider = document.getElementById(sliderId); - const output = document.getElementById(outputId); - // Calculate slider value and update output text +function updateOrganizationQuantity(organizationId) { + const slider = document.getElementById(organizationId); + const output = document.getElementById(`${organizationId}Output`); const sliderValue = Math.round(Math.pow(sliderScalar, slider.value)); output.textContent = `${sliderValue - 1} Trees`; - // Recalculate results calculateResults(); } @@ -59,7 +56,7 @@ for (let i = 1; i <= 3; i++) { const organizationId = 'organization' + i; // Update organization quantity when slider value changes document.getElementById(organizationId).addEventListener('input', function () { - updateOrganizationQuantity(organizationId, organizationId + 'Output'); + updateOrganizationQuantity(organizationId); }); } @@ -83,7 +80,7 @@ document.getElementById('numberOfTreesInput').addEventListener('input', function checkedOrganizations.forEach(organizationId => { const sliderId = organizationId.replace('Checkbox', ''); document.getElementById(sliderId).value = Math.log((numberOfTreesInputValue + 1) / checkedOrganizations.length) / Math.log(sliderScalar); - updateOrganizationQuantity(sliderId, sliderId + 'Output'); + updateOrganizationQuantity(sliderId); }); // Recalculate results @@ -110,7 +107,7 @@ document.getElementById('budgetInput').addEventListener('input', function () { const adjustedOrganizationQuantity = Math.floor(budgetInputValue / pricePerOrganization) / checkedOrganizations.length; document.getElementById(sliderId).value = Math.log(adjustedOrganizationQuantity + 1) / Math.log(sliderScalar); - updateOrganizationQuantity(sliderId, sliderId + 'Output'); + updateOrganizationQuantity(sliderId); }); // Recalculate results