MOD 8 - HASHING - version 2

modifications after posting from Dr. Eaglin

by SHELDON PASCIAK

HTML

This program takes a phrase, create bins for each word, and then records the instance position of each word in the bin. <br />

<br />

<!--


VERSION 2 - IN PROGRESS .....

    I HOPE TO HAVE IT REENGINEERED BEFORE 1 OCT - 

    NOTES/EXAMPLE FROM DR. EAGLIN, 25 SEP

var hashTable = new HashTable(); // Global
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]);
    };
}

function HashTable(){
    // Decide how to store the bins - add here
    // This will contain all the Words
    this.bins = []; // using array as example (list is better)
    this.addWord = function(word) {
        alert('Adding ' + word + ' to Hash Table');
        return word;
        // You will add a word to the HashTable
        // If word is in table add to Word
        // If not create new one    
    };
    
    this.hasWord = function(word) {
        // Implement
        return word;
    };
    
    this.addNewWord = function(word) {
        // Implement
        return word;
    };
    
    this.addExistingWord  = function(word) {
        // Adds Word to structure that already exists
        return word;
    };
}

function Word(word) {
    this.word = word; // store word
    this.indexes = []; // using array to store indexes
    
    // Probably want some logic here

    }


-----




  Module 8 -  Hashing Part 1

*** work..ing in progress for optimization/improvement. After seeing Dr. Eaglin's example posted today (25 sep), and after having submitted this much earlier (14 Sep) - I realize now I may need to further encapsulate -- I hope to find the time to come back to do this.  
 
The topic of hashing is quite easy Topic - Hashing Techniques - if you have done anything as simple as putting socks in one drawer and underwear in another,...

CSS

* {
    
    font: 14pt Arial;
}

JavaScript

// module 8 - program 1 - *** VERSION 2 *** sheldon pasciak


$("#startHashing").click( function() { 
    var myh = new HashTable(); // 
    var stringInput = $("#userInput").val(); 
	myh.addSentenceWords(stringInput);
    document.getElementById("output").innerHTML = myh.showBins();
});

function HashTable() {

    this.bins = []; // each bin contains unique word
    this.values = []; // matched index for bin with word // contains location indexes
        
    this.addWord = function(word) {    
        var theB = this.hasWord(word);        
        if (theB==-1) {
            this.bins.push(word);            
        }        
        theB = this.hasWord(word);        
        if (this.values[theB] == null) this.values[theB] = []; // create structure for new bin
        console.log("adding: " + word);        
        for (var i=0;i<this.bins.length;i++){
         console.log(this.bins[i]);// + " " + this.values[theB].length);//show words in bins   
        }
        return theB; // INDEX of the bin where word should be
    };
    
    //return index of bin that is named for the word or -1 if not found
    this.hasWord = function(word) {
        // Implement
        for (var i=0;i<this.bins.length;i++){
         if (this.bins[i]==word) return i;
        }
        return -1;
    };    
    
    this.showBins = function() {     
        for (var i=0;i<this.bins.length;i++) {
            var theb = this.values[i];
            console.log(this.bins[i]);
            for (var j=0;j<theb.length;j++) {
                console.log(theb[j]);
            }            
        }        
        var locations="";
        var sout="";        
        for (var i=0;i<this.bins.length;i++){            
            locations="";
            for (var j=0;j<this.values[i].length;j++){
                locations += " " + this.values[i][j];
            }
         	console.log(this.bins[i] + locations);   
            sout += this.bins[i] + ": " + locations + "<br />";
        }       ...