<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;
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.