JSFiddle - React, Tailwind, and code Playground
by Erik East
HTML
<h1>
Module9
</h1>
<h2>
Assignment 09
</h2>
<form id="form">Enter Phrase.
<br>
<textarea id='phrase' rows='10' cols='50'>this is some sample text this is more sample text ha ha</textarea>
<br>
<input type="button" value="Parse Phrase" id="parse" onClick='parsePhrase();'>
</form>
<div id="output"></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);
};
console.log(hashTable.bins);
hashTable.printHash();
}
function HashTable(){
this.bins = [];
this.addWord = function(word, position) {
//alert('Adding ' + word + ' to Hash Table');
if(this.hasWord(word)){
this.addExistingWord(word, position);
}else{
this.addNewWord(word, position);
}
return word;
};
this.hasWord = function(word) {
// Implement
for(var i=0; i<this.bins.length; i++){
if(this.bins[i].word == word){
return true;
}
}
return false;
};
this.addNewWord = function(word, position) {
// Implement
var potato = new funcWord(word, position);
this.bins.push(potato);
return word;
};
this.addExistingWord = function(word, position) {
// Adds Word to structure that already exists
for(var i=0; i<this.bins.length; i++){
if(this.bins[i].word == word){
this.bins[i].indexes.push(position);
}
}
return word;
};
this.printHash = function (){
document.getElementById('output').innerHTML = '';
var printer = '';
for(var i=0; i < this.bins.length; i++){
var word = this.bins[i];
printer += word.word + ': ' + word.indexes.join(', ')+ '<br/>';
}
document.getElementById('output').innerHTML = printer;
}
}
function funcWord(word, position) {
this.word = word; // store word
this.indexes = []; // using array to store indexes
...