Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 16 additions & 37 deletions Web-projects/Text-comparator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});