diff --git a/Web-projects/Text-comparator/script.js b/Web-projects/Text-comparator/script.js index bbee0f8..37c5b83 100644 --- a/Web-projects/Text-comparator/script.js +++ b/Web-projects/Text-comparator/script.js @@ -4,46 +4,25 @@ const textAreaList = document.querySelectorAll("textarea"); const buttonsList = document.querySelectorAll(".btn"); const lengthItems = document.querySelectorAll(".heading"); -const matched = (textArea) => { - if (textArea.classList.contains("matched")) return; - else if (textArea.classList.contains("un-matched")) { - textArea.classList.remove("un-matched"); - textArea.classList.add("matched"); - } else { - textArea.classList.add("matched"); - } -}; +const updateLengthAndMatchStatus = (index) => { + const textFromWindow1 = textAreaList[index].value; + const textFromWindow2 = textAreaList[index + 1].value; + + // Update the length display + lengthItems[index].textContent = `Length: ${textFromWindow1.length}`; + lengthItems[index + 1].textContent = `Length: ${textFromWindow2.length}`; -const unMatched = (textArea) => { - if (textArea.classList.contains("un-matched")) return; - else if (textArea.classList.contains("matched")) { - textArea.classList.remove("matched"); - textArea.classList.add("un-matched"); - } else { - textArea.classList.add("un-matched"); - } + // Determine match status + const isMatched = textFromWindow1 === textFromWindow2; + updateMatchClasses(textAreaList[index], isMatched); + updateMatchClasses(textAreaList[index + 1], isMatched); }; -const onclick = (index) => { - const textFromWindow1 = textAreaList[index].value; - const textFromWindow2 = textAreaList[index + 1].value; - lengthItems[index].textContent = `Length: ${textFromWindow1.length}`; - lengthItems[index + 1].textContent = `Length: ${textFromWindow2.length}`; - if (textFromWindow1 === textFromWindow2) { - matched(textAreaList[index]); - matched(textAreaList[index + 1]); - } else { - unMatched(textAreaList[index]); - unMatched(textAreaList[index + 1]); - } +const updateMatchClasses = (textArea, isMatched) => { + textArea.classList.toggle("matched", isMatched); + textArea.classList.toggle("un-matched", !isMatched); }; -buttonsList.forEach((btn) => { - btn.addEventListener("click", () => { - if (btn.classList.contains("btn--1")) { - onclick(0); - } else { - onclick(2); - } - }); +buttonsList.forEach((btn, index) => { + btn.addEventListener("click", () => updateLengthAndMatchStatus(index * 2)); });