Duplicate Word Checker
body {
background: linear-gradient(to right, #e3f2fd, #f1f8e9);
font-family: Arial, sans-serif;
}
textarea {
resize: none;
font-size: 1rem;
}
#outputText {
font-size: 1rem;
color: #333;
white-space: pre-wrap;
word-wrap: break-word;
}
.duplicate-word {
background-color: #ffeb3b;
color: #d32f2f;
font-weight: bold;
border-radius: 5px;
padding: 2px 5px;
}
document.getElementById("checkButton").addEventListener("click", function () {
const inputText = document.getElementById("inputText").value.trim();
const outputContainer = document.getElementById("outputText");
if (!inputText) {
outputContainer.innerHTML = "
Please enter some text to analyze.
";
return;
}
// Split words and count frequencies
const words = inputText.toLowerCase().split(/\s+/);
const wordCounts = words.reduce((counts, word) => {
counts[word] = (counts[word] || 0) + 1;
return counts;
}, {});
// Highlight duplicate words
const highlightedText = inputText.split(/\s+/).map(word => {
const lowerWord = word.toLowerCase();
return wordCounts[lowerWord] > 1
? `
${word}`
: word;
}).join(" ");
// Display the result
outputContainer.innerHTML = highlightedText;
});