Duplicate Word Checker In JavaScript

Category: Javascript , Text | April 13, 2022
Author:babaie774
Views Total:21 views
Official Page:Go to website
Last Update:April 13, 2022
License:MIT

Preview:

Duplicate Word Checker In JavaScript

Description:

The Duplicate Word Checker is a JavaScript-driven word counter that can detect how many times did you use one word in your article.

How to use it:

1. Code the HTML for the duplicate word check.

<form class="form">
  <div class="wordArea">
    <label class="label" for="word">Type Your Word To Check</label>
    <input class="formInput" id="formWord" name="word" type="text" onkeydown="getValWord()">
  </div>
  <div class="textArea">
    <label class="label" for="text">Your Text</label>
    <textarea class="fromTextArea" id="formText" rows="4" cols="50" onkeydown="getValText()"></textarea>
  </div>
  <input class="fromButton" id="formButton" type="button" value="Check">
</form>
<!-- Result -->
<p class="containerFinderResult" id="formResult"></p>

2. The main JavaScript to check how many times a word is repeated in your text.

// get least value word input
function getValWord() {
  return document.getElementById('formWord').value;
}
// get least value textarea input
function getValText() {
  return document.getElementById('formText').value;
}
//check logic code after onclick
const formButton = document.getElementById('formButton');
formButton.addEventListener('click', () => {
  let repeatTime =
      //if input was empty
      getValText() == '' & getValWord() == '' ? 'no' :
      //if input has value
      getValText().split(' ').filter((text) => {
      return text == getValWord().trim();
  }).length;
  document.getElementById('formResult').innerHTML = `your word repeated ${repeatTime} times`
});

You Might Be Interested In:


Leave a Reply