Hash function

by arosado417

HTML

<form id="form">Enter Phrase.
    <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

var hashTable = new HashTable(); // Global

function parsePhrase() {
    var phrase = document.getElementById('phrase').value;
    var phraseAsStringArray = phrase.split(' ');
    var len = phraseAsStringArray.length;
    hashTable.bins = [];
    for (var i = 0; i < len; i++) {
      //  alert(phraseAsStringArray[i]);
        hashTable.addWord(phraseAsStringArray[i],i+1);
    }
    //for (i = 0; i < len; i++){
    //console.log(hashTable.bins[i]);
for(var word in hashTable.bins){
  document.getElementById("output").innerHTML += word + " : " + hashTable.bins[word].index + (",") + "<br />";
}
   
    }
   

function HashTable(){
    // Decide how to store the bins 
    // This will contain all the Words
    this.bins = []; // using array as example (list is better)
    this.addWord = function(word,index) {
      //  alert('Adding ' + word + ' to Hash Table, remove this alert in your submission');
     var store = true; 
     var len = hashTable.bins.length;
     for(var i = 0; i < len; i++){
     for(var j = 0; j < hashTable.bins[i].length; j++){
      //run through the whole list adding the word and while adding checking to see if the word is already there and pushing the index 
     if(hashTable.bins[i][j] === word){
     hashTable.bins[i].push(index);
        // You will add a word to the HashTable
        // If word is in table add 
        // If not create new one    
        // return word and index;
     store = false;
     break;
      }
     }
    }
    //push the word and the index of every occurance into hashTable.bins[]
   if(store){
    var array = [];
    array.push(word);
    array.push(index);
    hashTable.bins.push(array);
   }
  }


 }
 
 /*If you look at some compression techniques (such as LZW Compression https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch ) they make use of the concept of hashing techniques to compress files. We are going to do something similar. Consider the phrase

To be or not to be, that is the question.

Even...