Dictionary

by vzufelt

HTML

<p id ="Instructions">
Words already in the dictionary: I, in, into, inlet, inn, inner, innate, ink.
</p><br/> Word to add:
<input type="textbox" id="X" value="" />
<input type="button" value="Add To Dictionary" onclick="AddTo();" />
<input type="button" value="Spell Check" onclick="Check();" />
<p id="output"></p>
<p id="output2"> </p>
<p id="output3">

</p>

JavaScript

function Trie(key) {
    this.key = key;
    this.value;
}

Trie.prototype.AddToTrie = function (name) {

    var node = this,
        nameLength = name.length,
        i = 0,
        currentLetter;

    for (i = 0; i < nameLength; i++) {
        currentLetter = name[i];
        node = node[currentLetter] || (node[currentLetter] = new Trie(currentLetter));
    }

    node.value = name;
    node.name = name;

};

Trie.prototype.SpellCheck = function (name) {

    var node = this,
        nameLength = name.length,
        i, node;

    for (i = 0; i < nameLength; i++) {
        if (!(node = node[name[i]])) break;
    }

    return (i === nameLength) ? 'Word Exists' : 'Word not found or not in dictionary';
};


var dict = new Trie();
dict.AddToTrie("I");
dict.AddToTrie("in");
dict.AddToTrie("into");
dict.AddToTrie("inlet");
dict.AddToTrie("inn");
dict.AddToTrie("inner");
dict.AddToTrie("innate");
dict.AddToTrie("ink");






function AddTo() {
document.getElementById("output2").innerHTML = "";
var Word = document.getElementById("X").value;
if (Word == 0){var A ="Please Enter A Value";
return A;}

dict.AddToTrie(Word);
document.getElementById("output").innerHTML= "Word Added to dictionary: " + Word; 

}


function Check() {
document.getElementById("output").innerHTML = "";
document.getElementById("output2").innerHTML = "Word Checked: ";
var Word = document.getElementById("X").value;

if (Word == 0) { var A = "Please Enter A Value";
return A;}

document.getElementById("output").innerHTML = dict.SpellCheck(Word);
document.getElementById("output2").innerHTML += Word;

function Find(){
document.getElementById("output3").innerHTML = "Words simular to your search: ";
}


}