The Doc Dialer, Part 2: A Browser Independent Version: Prompting the User | WebReference

The Doc Dialer, Part 2: A Browser Independent Version: Prompting the User


The Doc Dialer, Part 2: A Browser Independent Version

Prompting the User

One of the new features of this column is the ability to prompt the user for new names and extension numbers. The function enterNewName() does not take any parameters and checks that the entered name is not a duplicate of an entry already in the system:


function enterNewName() {
  var aDuplicate = true;
  while (aDuplicate) {
    var newName = prompt("Enter A New Name, First Name followed by
     Last Name, blank separated:", "Theodore Roosevelt");
  // (The two lines above should be joined as one line.
  // They have been split for formatting purposes.)
    if (!newName) return;
    var firstName = extractFirstName(newName);
    var lastName = extractLastName(newName);
    while (true) {
      var phoneExt = parseInt(prompt("Enter employee's 4-digit
        extension number:", "0000"));
  // (The two lines above should be joined as one line.
  // They have been split for formatting purposes.)
      if (phoneExt >= 1000 && phoneExt 

We begin with setting the variable aDuplicate to true:

var aDuplicate = true;

We keep prompting the user until a non-duplicate name is entered. First we prompt the user for the new name:


var newName = prompt("Enter A New Name, First Name followed by
  Last Name, blank separated:", "Theodore Roosevelt");
  // (The two lines above should be joined as one line.
  // They have been split for formatting purposes.)

If the user cancels this prompt box, we return immediately to the main script:

if (!newName) return;

We then extract the first and last names from the prompt box entry:

var firstName = extractFirstName(newName);
var lastName = extractLastName(newName);

We then go an infinite loop for prompting the new employee's extension number. We break the loop only when the entered extension is a 4-digit number:


while (true) {
  var phoneExt = parseInt(prompt("Enter employee's 4-digit
   extension number:", "0000"));
  // (The two lines above should be joined as one line.
  // They have been split for formatting purposes.)
  if (phoneExt >= 1000 && phoneExt 

Still to be checked is whether the new name-extension combination is a duplicate of an existing name-extension combination. The new name is comprised of the first and last name with a blank delimiter in between:

empList[lastEmp] = firstName + " " + lastName;

We then call the verifyNewName() function which takes two parameters: the new name and the new extension:

aDuplicate = verifyNewName(checkNewName, phoneExt);

We'll describe this function on the next page. Once the entered name-index combination is not a duplicate, aDuplicate becomes false and the while(aDuplicate) loop is broken. What's left to be done is to increment the number of entries:

lastEmp++; 

Store the new name in the empList array:

empList[lastEmp] = firstName + " " + lastName;

Store his or her extension:

empPhone[lastEmp] = phoneExt;

Finally, we can add the new entry to the trie data structure. First we enter the first name followed by the last name:

addToTrie(firstName + lastName, lastEmp);

And then the last name followed by the first name:

addToTrie(lastName + firstName, lastEmp);

Next: How to search for a name

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

Created: February 28, 2000
Revised: April 26, 2000

URL: http://www.webreference.com/js/column58/6.html