JSFiddle - React, Tailwind, and code Playground
by leiperte
HTML
Enter a word: <input type = "textbox" id = "input" onkeyup = "findword();"/>
<input type = "button" id = "add" value = "Add to Dictionary" onClick = "add()"/>
<input type = "button" id = "check"value = "Spell Check" onClick = "check()"/>
<div id = "output">
</div>
JavaScript
//Assignment 13
function Trie(key){
this.key = key;
}
Trie.prototype.AddToTrie = function(word){
var node = this;
var wordLength = word.length;
var i = 0;
var currentLetter;
for(i=0; i < wordLength; i++){
currentLetter = word[i];
node = node [currentLetter] || (node[currentLetter] = new Trie(currentLetter));
}
node.value = word;
node.word = word;
}
Trie.prototype.SpellCheck = function(word){
var node = this;
var wordLength = word.length;
var i = 0;
for(i = 0; i < wordLength; i++){
if (!(node = node[word[i]])) break;
}
return (i == wordLength) ? "In the dictionary. " : "Not in the dictionary, check your spelling. ";
}
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 add(){
document.getElementById("output").innerHTML = " ";
var input = document.getElementById("input").value;
dictionary.AddToTrie(input);
document.getElementById("output").innerHTML = "Word Added: " + input;
}
function check(){
document.getElementById("output").innerHTML = "";
document.getElementById("output").innerHTML = "Word Checked: ";
var input = document.getElementById("input").value;
document.getElementById("output").innerHTML = dictionary.SpellCheck(input);
document.getElementById("output").innerHTML += "Your word was: " + input;
}
var words = ["I", "in", "into", "inlet", "inn", "inner", "innate", "ink"];
function displaywords(input, words){
var reg = new RegExp(input.split("").join('\\w*').replace(/\W/, ""), 'i');
return words.filter(function(input){
if(input.match(reg)){
return input;
}
});
}
function findword(input){
var display = displaywords(input);
document.getElementById("output").innerHTML = display;
}