RB_JSFIDDLE_A13
by Ryan Brown
HTML
<form id = "formski">
<p>
Please Enter a Word to add to the List and then use the Spell Check to see if it has been accurately entered.
</p>
<input type = "textbox" id = "userEntry" value = "I" />
<br/>
<br/>
<input type = "button" id = "btn" value = "Add Word To List"onclick = "addWord();"/>
<input type = "button" id = "btn" value = "Spellchecker" onclick = "checkSpelling();"/>
<p id ="output"></p>
<p id ="outputSec"></p>
</form>
CSS
#formski, #btn
{
font-family: courier;
}
JavaScript
function Trie(uWord)
{
this.word = uWord;
this.value;
}
Trie.prototype.addToTrie = function (uWord)
{
var node = this;
var uWordLength = uWord.length;
var i = 0;
var currentLetter;
for (i = 0; i < uWordLength; i++)
{
currentLetter = name[i];
node = node[currentLetter] || (node[currentLetter] = new Trie(currentLetter));
}
node.value = name;
node.name = name;
};
Trie.prototype.SpellCheck = function (uWord)
{
var node = this;
var uWordLength = name.length;
var i;
var node;
for (i = 0; i < uWordLength; i++)
{
if (!(node = node[name[i]])) break;
}
return (i === uWordLength) ? "Entry Has been found in the dictionary, Thank you." : "Value not found or was spelled incorrectly";
};
var dictionary = new Trie();
dictionary.addToTrie("I");
dictionary.addToTrie("in");
dictionary.addToTrie("into");
dictionary.addToTrie("inlet");
dictionary.addToTrie("inn");
dictionary.addToTrie("inner");
dictionary.addToTrie("innate");
dictionary.addToTrie("ink");
function addWord()
{
document.getElementById("outputSec").innerHTML = "";
var uValue = document.getElementById("userEntry").value;
if (uValue == 0)
{
var inc = "Please enter something.";
return inc;
}
dictionary.addToTrie(uValue);
document.getElementById("output").innerHTML = "Word added by user: " + uValue;
}
function checkSpelling()
{
document.getElementById("output").innerHTML = "";
document.getElementById("outputSec").innerHTML = "Word Checked: ";
var uValue = document.getElementById("userEntry").value;
if (uValue == 0)
{
var inc = "Please Enter A Value";
return inc;
}
document.getElementById("output").innerHTML = dictionary.SpellCheck(uValue);
document.getElementById("outputSec").innerHTML += uValue;
}