Assignment 9

by Taylor Zimmerman

HTML

<form id="form">Enter Phrase.
    <br>
    <textarea id='phrase' rows='10' cols='50'>A hash function is any function that can be used to map data of arbitrary size to data of fixed size.</textarea>
    <br>
    <input type="button" value="Parse Phrase" id="parse" onClick='parsePhrase();'>
</form>
<fieldset>
<legend>Phrase Result</legend>
<div id="output"></div>
</fieldset>

JavaScript

var hashTable = new HashTable(); // Global
var d = "";
function parsePhrase() {
    var phrase = document.getElementById('phrase').value;
    var phraseAsStringArray = phrase.split(' ');
    var len = phraseAsStringArray.length;
    
    for (var i = 0; i < len; i++) {
        //alert(phraseAsStringArray[i]);
        hashTable.addWord(phraseAsStringArray[i]);
    }
    document.getElementById('output').innerHTML = hashTable.print();
};

function HashTable(){
    // Decide how to store the bins - add here
    // This will contain all the Words
  var binLength = 1;
	var binNum = 1;
  var Bin = function (word) {
  	this.next = null;
    this.content = word;
    this.index = binNum;
  };
   
    this.addWord = function(word) { 
    	if(this.head == null){
        hashTable.addNewWord(word);
      }
      else {
      	hashTable.hasWord(word);
      }
      return word;
           
    };
    
    this.hasWord = function(word) {
        var newWord = 0;
        var headWord = this.head;
				for(i = 0; i < binLength; i++){
        	if(word == headWord.content){ 
          	newWord = 1;
          }
          if(headWord  != null){
          headWord = headWord.next;
          }      
        }
        if(newWord == 0){
        	hashTable.addNewWord(word);
        } else {
        	hashTable.addExistingWord(word);
        }
        return word;
        
    };
    
    this.addNewWord = function(word) {
        if (this.head == null) {
            this.head = new Bin(word);
            this.index = binNum;
            binNum++;
            return this.head;
        }
        if (this.tail == null) {
            this.tail = new Bin(word);
            this.head.next = this.tail;
            this.index = binNum;
            binNum++;
            return this.tail;
        };
        this.tail.next = new Bin(word);
        this.tail = this.tail.next;
        this.tail.next = null;
        this.index = binNum;
        binLength++;
        binNum++;
        return this.tail;
  ...