Monday, April 3, 2023

Duplicate Word Checker in HTML with JS for web page




Below this code creates a simple webpage with a textarea for entering text, a button for triggering the duplicate word checker, and a result area for displaying the results.

 
The JavaScript code
uses a split() method to split the text into an array of words, then creates a dictionary to keep track of the number of times each word appears. Finally, it loops through the dictionary and adds any words with a count greater than one to the result area.



The code is also responsive, thanks to the use of the viewport meta tag, which ensures that the webpage will scale properly on different screen sizes.




Copy full script Here  👇








<!DOCTYPE html>
<html>
<head>
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title>Duplicate Word Checker</title>
</head>
<body>
 <h1>Duplicate Word Checker</h1>
 <label for="text-input">Enter some text:</label>
 <textarea id="text-input" rows="5" cols="50"></textarea>
 <button onclick="checkDuplicate()">Check for duplicates</button>
 <p id="result"></p>

 <script>
  function checkDuplicate() {
   var text = document.getElementById("text-input").value;
   var words = text.toLowerCase().split(/\W+/);
   var duplicateWords = {};
   var result = "";

   for (var i = 0; i < words.length; i++) {
    if (duplicateWords.hasOwnProperty(words[i])) {
     duplicateWords[words[i]]++;
    } else {
     duplicateWords[words[i]] = 1;
    }
   }

   for (var word in duplicateWords) {
    if (duplicateWords[word] > 1) {
     result += "The word '" + word + "' appears " + duplicateWords[word] + " times.<br>";
    }
   }

   if (result === "") {
    result = "No duplicate words found.";
   }

   document.getElementById("result").innerHTML = result;
  }
 </script>
</body>
</html>



No comments:

Post a Comment

10 Online Jobs for College Students to Earn Money

Title: 10 Online Jobs for College/University Students to Earn Money In today's digital age, earning money online has become easier tha...