spell check

by chris richarde

HTML

<input type="button" value="Add word" onclick="spelltrie.addNew()" />
<input type="button" value="Spell Check" onclick="spelltrie.spellCheck()" />
<br/> 
<input type="textbox" id="Y" value=" " />
<p id="output"></p>
<p id="output2"></p>

JavaScript

function trie(){
	this.words=0;
  this.prefixes=0;
  this.children=[];
}

trie.prototype.add =function (word,pos){ 
  var k;
	var child;
  if (word.length==0){
  	return;
  }
	if (pos == undefined){
  	pos = 0;
  }
	if (pos == word.length){
  	this.words++;
    return;
  }
	this.prefixes++;
  k=word[pos];
  if(this.children[k]==undefined){
  	this.children[k]=new trie();
  }
	child=this.children[k];
  child.add(word, pos +1);
}
trie.prototype.counts=function (word,pos){
	var count = 0;
	var k;
	var child;
  
  if (word.length==0){
  	count=0;
  }
	if(pos== undefined){
  	pos=0;
  }
	if (pos == word.length){
  	count=this.words;
  }
	k=word[pos];
  child= this.children[k];
  if(child != undefined){
  	count = child.counts(word,pos+1);
  }
	return count;
}

trie.prototype.create = function() {
    this.add("I");
    this.add("in");
    this.add("into");
    this.add("inlet");
    this.add("inn");
    this.add("inner");
    this.add("innate");
    this.add("ink");
}

trie.prototype.print=function(word){
	var k;
  var child;
  var s = "";
  var array=[];
  if (word == undefined){
  	word="";
  }
  if(this==undefined){
  	return[];
  }
  if (this.words > 0){
  	array.push(word);
  } 
	for (k in this.children){
  	child=this.children[k];
    array= array.concat(child.print(word+k));
  } 
	for (var i =0; i<array.length;i++){
  	s+=array[i]+ " ";
  }
	return s;
}

trie.prototype.addNew=function(word,position){
	clearDisplay();
  var word=document.getElementById("Y").value;
  if(word.length==0||!isNaN(word)){
  document.getElementById("output").innerHTML +="please enter word containing no numbers and at least one letter";
  }
	else{
  this.add(word);
  document.getElementById("output").innerHTML = word + " added";
   }
   document.getElementById("output2").innerHTML += this.print();
}

trie.prototype.spellCheck=function(word){
	var word=document.getElementById("Y").value;
	if(word.length==0||!isNaN(word)){
  	document.getElementById("output").innerHTML +="please enter...