The Doc Dialer: The Global Arrays
The Doc Dialer
The Global Arrays
There are several global arrays we use in our DOCJS Trie Phone. Their size is determined by the global variable EMPMAX:
EMPMAX = 100;
The first array, empList
, holds the president names:
var empList = new Array(EMPMAX);
empList[1] = "Ronald Reagan"; //76 //73
empList[2] = "Jimmy Carter"; //54 //22
empList[3] = "Richard Nixon"; //74 //6
empList[4] = "Bill Clinton"; //24 //25
empList[5] = "Gerald Ford"; //437 //3
empList[6] = "George Bush"; //436 //28
empList[7] = "Lyndon Johnson"; //59 //56467
empList[8] = "John Kennedy"; //56465 ///53
The comments on the right hand side of the names show the trie indexing for the first name + last name and for the last name + first name indexing. Bill Clinton, for example, is indexed on 24 by BillClinton, and on 25 by ClintonBill. When we store the presidents on the trie data structure, we use their index in this empList
array. The assignment mentioned in the previous page:
tree[7][4] = 3; // Richard Nixon
refers to Richard Nixon's position in the empList
array (3). The second global array, empPhone
, holds a four-digit extension numbers. These can be, for example, internal extensions in a company phone system, or entries to an Internet-based data base as is the case we though about in our DOCJS Trie Phone. Here are the extension:
var empPhone = new Array(EMPMAX);
empPhone[1] = 5808;
empPhone[2] = 5919;
empPhone[3] = 5303;
empPhone[4] = 5606;
empPhone[5] = 5707;
empPhone[6] = 5313;
empPhone[7] = 5838;
empPhone[8] = 5006;
The currentTrie
variable points to the current trie position. For each phone key clicked, we need to step down one level of hierarchy. Therefore, we need to remember the array element that we are currently standing on. We initialized this variable to start at the root of the trie, the top-level array object:
var currentTrie = tree;
Since our DOCJS Trie Phone works with both first name + last name as well as last name + first name, we need to avoid duplication of matches. For example, we don't want to display Ronald Reagan twice when the user presses the 7 key. The printList
array takes care of it. It is also initialized to have a size of EMPMAX
. We assign a false value to all array elements before we start descending the trie, and we switch on every name that needs to be displayed. Before letting the user key in his or her name, we clear the array:
clearDisplayIndex();
The last global variable is welcomeStr
. It is the message we print to the display after every match:
var welcomeStr = "Welcome to DOCJS Trie Phone.
Please key in a president name:";
Finally, we print it to the display:
outputStringToDisplay(welcomeStr);
Produced by Yehuda Shiran and Tomer Shiran
Created: February 14, 2000
Revised: February 14, 2000
URL: https://www.webreference.com/js/column57/4.html