Assignment 13

by aren_anderson

HTML

Word to add:
<input type="text" id="input" onkeyup="changeInput(value)">

<input type="button" value="Add To Dictionary" onclick="add();" />
<input type="button" value="Spell Check" onclick="checkSpelling();" />
<p id="output1"></p>
<p id="output2"></p>
<p id="output3"></p>

CSS

<input type="textbox" id="input" onkeyup="getInput()" />

JavaScript

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

Trie.prototype.AddToTrie = function(word) {
  var node = this;
  var wordLength = word.length;
  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;

  for (var i = 0; i < wordLength; i++) {
    if (!(node = node[word[i]])) break;
  }
  return (i == wordLength) ? 'Value Exists' : 'Value not found or spelled incorrectly';
}

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 add() {
  document.getElementById("output3").innerHTML = "";
  var input = document.getElementById("input").value;
  dict.AddToTrie(input);
  document.getElementById("output2").innerHTML = "Word Added = " + input;
}

function checkSpelling() {
  document.getElementById("output2").innerHTML = "";
  document.getElementById("output3").innerHTML = "Word Checked: ";
  var input = document.getElementById("input").value;
  document.getElementById("output2").innerHTML = dict.SpellCheck(input);
  document.getElementById("output3").innerHTML += input;
}

var people = ["I", "in", "into", "inlet", "inn", "inner", "innate", "ink"];

function matchPeople(input) {
  var reg = new RegExp(input.split('').join('\\w*').replace(/\W/, ""), 'i');
  return people.filter(function(input) {
    if (input.match(reg)) {
      return input;
    }
  });
}

function changeInput(input) {
  var autoCompleteResult = matchPeople(input);
  document.getElementById("output1").innerHTML = autoCompleteResult;
}