JSFiddle - React, Tailwind, and code Playground

by Peyton Hessler

HTML

<textarea id='phrase' rows='10' cols='50'>Click the enter button to have your phrase parsed. The program will take what you enter here and print a number next to the word that shows where that word appears in the sentence.</textarea>
  <br>
  <input type="button" value="ENTER" id="parse" onClick='parsePhrase();'>
<div id="output"></div>

JavaScript

// This program is fairly easy to understand. The words put into the text box are all separted into the their own category. Each word that is the same is put into the same bin. Those words are then compared to see where they were placed in the phrase and then printed onto the screen to show what position they are in.

var hashTable = new HashTable();


// I'm going to take the phrase that was entered into the text box and hash it. I will store it inn bins to be compared later and used to count at what position they are at.
function parsePhrase() {
  hashTable.bins = [];
	
  var phrase = document.getElementById('phrase').value;
  phrase = phrase.toLowerCase();
  var phraseAsStringArray = phrase.split(' ');
  var len = phraseAsStringArray.length;

  for (var i = 0; i < len; i++) {

    hashTable.addWord(phraseAsStringArray[i], i + 1);
  }

  print();
}


// This is going to see if the word compares and will add on to an existing word, but if it does not then it will add a new word in the else statement.
function HashTable() {

  this.bins = [];

  this.addWord = function(word, position) {

    if (this.hasWord(word) == true) {
      this.addExistingWord(word, position);
    } else {
      this.addNewWord(word, position); //add new word
    }


    return word;
  };


// This will basically see if the hashed word is in the bin and make sure that the words are all the same.
  this.hasWord = function(word) {
    var x = false;

    for (var i = 0; i < this.bins.length; i++) {
      var hashedWord = this.bins[i].split(":");

      if (hashedWord[0] == word) {
        x = true;
      }
    }


    return x;
  };

// This function is going to add a new word and the position that it is in. It won't add where the word occurs in a new position and there is a function for that below.
  this.addNewWord = function(word, position) {

    var NewWord = word + ": " + position;
    this.bins.push(NewWord);
    return word;
  };


// This function is going to add where an existing...