JSFiddle - React, Tailwind, and code Playground

A9

by Tori Hoelscher

HTML

<form id="form">Ennyn Durin Aran Moria. Pedo Mellon a Minno. Im Narvi hain echant. Celebrimbor o Eregion teithant i thiw hin.
<br>
<br>Speak Friend and Enter<br/>
<br>Enter the Phrase <br/>
<form>
<br>
<textarea id='phrase' rows='10' cols='50'>I want what I want and I know what I want</textarea>
<br>
<input type="button" value="Parse Phrase" id="parse" onClick='parsePhrase();'>
</form>
<div id="output"></div>

JavaScript

function parsePhrase() {
  var hashTable = new HashTable();
  var phrase = document.getElementById('phrase').value;
  var phraseStringArray = phrase.split(' ');
  var len = phraseStringArray.length;

//create variables 
  for (var i = 0; i < len; i++) {
    hashTable.addWord(phraseStringArray[i],i+1);
  };
  
  var output = '<br/>';
  var lenbins = hashTable.binsWord.length;
  for (var i = 0; i < lenbins; i++) {
    output += hashTable.binsWord[i]+': ';
    output += hashTable.binsWordLoca[i];
    output += "<br/>";
  };
//create hash table
  document.getElementById("output").innerHTML = output;
  generateReport();
}
//generate the report 
function HashTable(){
  this.binsWord = [];
  this.binsWordLoca = [];
  this.addWord = function(word,wordLoca) {
  
    var foundAtIndex= this.hasWord(word);
    if (foundAtIndex == 'n'){
      this.addNewWord(word,wordLoca)
    }else{
      this.addExistingWord(word,wordLoca,foundAtIndex)
    }
    return word;
  };
//
  this.hasWord = function(word) {
    var len = this.binsWord.length;
    var wordFound = 'n';
    for (var i = 0; i < len; i++) {
      if (this.binsWord[i] == word){
        wordFound = i;
      }
    }
    return wordFound;
  };

  this.addNewWord = function(word,wordLoca) {
    var newIndex = this.binsWord.length;
    this.binsWord [newIndex] = word;
    this.binsWordLoca [newIndex] = wordLoca;
    return word;
  };

  this.addExistingWord  = function(word,wordLoca,foundAtIndex) {

    this.binsWordLoca [foundAtIndex] = this.binsWordLoca [foundAtIndex] + ' ' + wordLoca;
    return word;
  };
}