spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column57


The Doc Dialer

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

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<=9; i++) {
      memberNode = trieNode[i];
      if (memberNode) {
        if (typeof(memberNode) == "number") {
          addEmployeeToDisplay(memberNode);
        }
        else {
          updateBoard(memberNode);
        }
      }
    }
  }
}

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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business


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

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