//Assignent 9
var bins = []; //stores each of the words once
var phraseAsStringArray = []; // stores the words as the occur in the phrase
//function to create list items
function makeNode(value) {
this.id = 0;
this.content = value;
this.next = null;
this.last = null;
}
//The lists are holding the positions of the words for each bucket
function List(value) {
this.head = new makeNode(value);
this.last = this.head;
}
List.prototype.addNode = function(value) {
if (this.head == null) {
this.head = new makeNode(value);
return this.head;
}
if (this.last == null) {
this.last = new makeNode(value);
this.head.next = this.last;
return this.last;
}
var newNode = new makeNode(value);
this.last.next = newNode;
newNode.last = this.last;
this.last = newNode;
this.id++;
}
//preparing for the print function
makeNode.prototype.asString = function() {
return this.content + " ";
}
//the print function for the list
List.prototype.print = function() {
var Content = '<br/>';
var node = this.head;
while (node != null) {
Content += node.asString();
node = node.next;
}
return Content;
}
//creating the hashtable
var hashTable = new HashTable(); // Global
//cutting the phrase and storing the words into an array
function parsePhrase() {
var phrase = document.getElementById('phrase').value;
phrase = phrase.toLowerCase();
phrase = phrase.replace(/[^a-zA-Z0-9]/g, ' ');
//phrase = phrase.replace(' ', '');
phraseAsStringArray = phrase.split(' ');
var len = phraseAsStringArray.length;
for (var i = 0; i < len; i++) {
hashTable.addWord(phraseAsStringArray[i] + ":", i + 1);
}
output();
}
//the hashtable holds the arry of lists
function HashTable() {
this.bins = []; //this array will be filled with linked lists
//the add word function
this.addWord = function(word, index) {
if (this.hasWord(word)) //if the word exists ...
{
...
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.