<input type="button" value="create list" id="parse" onClick='Create();'>
<br/>
<br/> List
<br/>
<div id="output1"></div>
<br/> List and Hashes
<br/>
<div id="output2"></div>
<br/> Index
<br/>
<div id="output3"></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);
};
hashTable.print();
}
function HashTable(){
this.bins = [];
this.addWord = function(word, loc) {
if(this.hasWord(word)){
this.addExistingWord(word, loc);
}
else{
this.addNewWord(word,loc);
}
return word;
};
this.hasWord = function(word) {
for(var i=0; i<this.bins.length; i++){
var L = word.toLowerCase();
var U = word.toUpperCase();
if(this.bins[i].word == word ||this.bins[i].word == word.toLowerCase() ||this.bins[i].word == word.toUpperCase() ){
return true; // couldn't get the if statement to include upper and lower case in boundaries for being the same word.actually sometimes they will disappear if they are upper and lower case copies.
}
}
return false;
};
this.addNewWord = function(word, loc) {
var x = new Word(word, loc);
this.bins.push(x);
return word;
};
this.addExistingWord = function(word, loc) {
for(var i=0; i<this.bins.length; i++){
if(this.bins[i].word == word){
this.bins[i].num.push(loc);
}
}
return word;
};
this.print = function (){
for(var i=0; i < this.bins.length; i++){
var word = this.bins[i];
document.getElementById('output').innerHTML+= word.word + ' => ' + word.num + '<br/>';
}
}
}
function Word(word, loc) {
this.word = word; // store word
this.num=[];
this.num.push(loc);
// Probably want some logic here
}
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.