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:

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: