spacer

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

home / experts / javascript / column57


The Doc Dialer

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


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

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