The Doc Dialer: Extracting the Data Elements | WebReference

The Doc Dialer: Extracting the Data Elements


The Doc Dialer

Extracting the Data Elements

The key function of our DOCJS Trie Phone is updateBoard(). It accepts one parameter, the current trie object, and collects all data elements that are stored below it, somewhere in the trie. Here is the function:

function updateBoard(trieNode) {
  if (typeof(trieNode) == "number") {
    addEmployeeToDisplay(trieNode);
  }
  else {
    for (i=2; i

This function is a recursive one (calling itself). When you have a recursive function, you need to have a termination condition, or else the function will run forever. The termination condition is expressed in the top section. If the type of the node is "number", it is an indication that we arrived at a name and can add it to the display:

if (typeof(trieNode) == "number") {
  addEmployeeToDisplay(trieNode);
}

If the type of the node is not "number", it means that we have 10-element array object which we need to scan for possible branches of the trie. We assign each element to memberNode:

memberNode = trieNode[i]

And then check that it is not a null object:

if (memberNode)

As explained in the previous page, we need to act differently when the type of the node is "number" as oppose to "object". In the first case, it is a termination indication and we just add the name to the display:

if (typeof(memberNode) == "number") {
  addEmployeeToDisplay(memberNode);
}

Otherwise, we call updateBoard() recursively:

else {
  updateBoard(memberNode);
}

The function addEmployeeToDisplay() just turns on the indicated member in the printList array:

function addEmployeeToDisplay(empNum) {
  printList[empNum] = true;
}

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

Created: February 14, 2000
Revised: February 14, 2000

URL: http://www.webreference.com/js/column57/7.html