<form id="form">Ennyn Durin Aran Moria. Pedo Mellon a Minno. Im Narvi hain echant. Celebrimbor o Eregion teithant i thiw hin.
<br>
<br>Speak Friend and Enter<br/>
<br>Enter the Phrase <br/>
<form>
<br>
<textarea id='phrase' rows='10' cols='50'>I want what I want and I know what I want</textarea>
<br>
<input type="button" value="Parse Phrase" id="parse" onClick='parsePhrase();'>
</form>
<div id="output"></div>
JavaScript
function parsePhrase() {
var hashTable = new HashTable();
var phrase = document.getElementById('phrase').value;
var phraseStringArray = phrase.split(' ');
var len = phraseStringArray.length;
//create variables
for (var i = 0; i < len; i++) {
hashTable.addWord(phraseStringArray[i],i+1);
};
var output = '<br/>';
var lenbins = hashTable.binsWord.length;
for (var i = 0; i < lenbins; i++) {
output += hashTable.binsWord[i]+': ';
output += hashTable.binsWordLoca[i];
output += "<br/>";
};
//create hash table
document.getElementById("output").innerHTML = output;
generateReport();
}
//generate the report
function HashTable(){
this.binsWord = [];
this.binsWordLoca = [];
this.addWord = function(word,wordLoca) {
var foundAtIndex= this.hasWord(word);
if (foundAtIndex == 'n'){
this.addNewWord(word,wordLoca)
}else{
this.addExistingWord(word,wordLoca,foundAtIndex)
}
return word;
};
//
this.hasWord = function(word) {
var len = this.binsWord.length;
var wordFound = 'n';
for (var i = 0; i < len; i++) {
if (this.binsWord[i] == word){
wordFound = i;
}
}
return wordFound;
};
this.addNewWord = function(word,wordLoca) {
var newIndex = this.binsWord.length;
this.binsWord [newIndex] = word;
this.binsWordLoca [newIndex] = wordLoca;
return word;
};
this.addExistingWord = function(word,wordLoca,foundAtIndex) {
this.binsWordLoca [foundAtIndex] = this.binsWordLoca [foundAtIndex] + ' ' + wordLoca;
return word;
};
}
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.