Hash Compression

We are going to create bins for each word, and record the position of the instances of the words in the proper bins. "To be or not to be, that is the question." So write a program that first has a series of bins, Because the word to occurs in positions 1 and 5, the bin for the word to will contain 1 and 5. Write an program that will take the phrase, create bins for each word, and then record the instance position of each word in the bin. It will then output each bin (word) and the locations of the word in the phrase.

by Neil Daley

HTML

<H2>
Hashing
</H2>

Click "Parse Phrase" and the default text in the text area will be parsed. <br/><br/>
You can also enter a new phrase into the text area and Parse Phrase that text.<br/><br/>

<form id="form">Enter Phrase.
    <br>
    <textarea id="phrase" rows="10"cols="50">I have found that if you love life life will love you back</textarea>
    <br>
    
    <input type="button" value="Parse Phrase" id="parse" onClick="parsePhrase()" style="color:white; background-color:blue" />
    
    <input type="button" id="buttonClear" value="Clear List" onclick="clearScr()" style="color:white; background-color:blue" />
    
</form>
<div id="output"></div>

JavaScript

var buckets = []; // stores each word
var phraseStringArray = []; // stores the words as the occur in the phrase

// create list items
function makeNode(value) {
  this.id = 0;
  this.content = value;
  this.next = null;
  this.last = null;  
}

// List will hold position of the words for each bucket
function List(value) {
  this.head = new makeNode(value);
  this.last = this.head;
}
List.prototype.addNode = function(value) {
  if (this.head == null) {
    this.head = new makeNode(value);
    return this.head;
  }
  if (this.last == null) {
    this.last = new makeNode(value);
    this.head.next = this.last;
    return this.last;
  }
  var newNode = new makeNode(value);
  this.last.next = newNode;
  newNode.last = this.last;
  this.last = newNode;
  this.id++;
}

// print function prep
makeNode.prototype.asString = function() {
  return this.content + " ";
}

// print function for the list
List.prototype.print = function() {
  var Content = "<br/>";
  var node = this.head;
  while (node != null) {
    Content += node.asString();
    node = node.next;
  }
  return Content;
}

// creating the hashtable
var hashTable = new HashTable(); // Global

// disecting phrase and storing word into array
function parsePhrase() {
  var phrase = document.getElementById("phrase").value;
  phraseStringArray = phrase.split(" ");
  var len = phraseStringArray.length;

  for (var i = 0; i < len; i++) {    
    hashTable.addWord(phraseStringArray[i] + ":", i + 1);    
  }
 output();  
}

// hashtable holding arry of lists
function HashTable() {  
  this.buckets = []; // array of linked lists
  // addWord function
  this.addWord = function(word, index) {    
    if(this.hasWord(word)) //if the word exists do instructions
    {
    	this.addExistingWord(word, index); // call addExistingWord.      
    }
    else {
    	this.addNewWord(word, index); // call to addNewWord again      
    }
    return word;    
  }
  
// detection for if-else statement
this.hasWord = function(word, index)...