Assignment 13

by Jeremy Boss

HTML

<input id=userIn type=text>
<button id=adDict onclick=adDictIn()>Add To Dictionary</button>
<button id=valid onclick=validate()>Spell Check</button>
<p id='out'></p>

JavaScript

document.getElementById('userIn').focus();

function Node(content) {
  this.content = content;
  this.varify = false;
  this.ar = [];
}

function Trie() {
  this.R = new Node();
}

Trie.prototype.adDict = function(val) {
  if (this.R == null) {
    return;
  }
  this.aNode(this.R, val);
};

Trie.prototype.aNode = function(node, val) {
  if (node == false || val == false) {
    return null;
  }
  var character = val.charAt(0);
  var ind = node.ar[character];
  if (ind == null) {
    ind = new Node(character);
    node.ar[character] = ind;
  }
  var rem = val.substring(1);
  if (rem == 0) {
    ind.varify = true;
  }
  this.aNode(ind, rem);

  Trie.prototype.contains = function(val) {
    if (this.R == null) {
      return;
    }
    return this.content(this.R, val);
  };
  Trie.prototype.content = function(node, val) {
    if (node == null || val == null) {
      return false;
    }
    var character = val.charAt(0);
    var ind = node.ar[character];
    if (ind) {
      var rem = val.substring(1);
      if (rem == false && ind.varify) {
        return true;
      } else {
        return this.content(ind, rem);
      }
    } else {
      return false;
    }
  }
};

var trie = new Trie();

function adDictIn() {
  var i = document.getElementById('userIn').value;
  trie.adDict(i);
}

function validate() {
  var s = document.getElementById('userIn').value;
  if (trie.contains(s)) {
    document.getElementById('out').innerHTML = 'Entry Found In Dictionary';
    document.getElementById('userIn');
  } else {
    document.getElementById('out').innerHTML = 'Entry NOT Found In Dictionary!';
    document.getElementById('userIn');
  }
}

trie.add('I');
trie.add('in');
trie.add('into');
trie.add('inlet');
trie.add('inn');
trie.add('inner');
trie.add('innate');
trie.add('ink');