spacer

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

home / experts / javascript / column58


The Doc Dialer, Part 2: A Browser Independent Version

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

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 <= 9999) break;
    }
    checkNewName = firstName + " " + lastName;
    aDuplicate = verifyNewName(checkNewName, phoneExt);
  }
  lastEmp++;
  empList[lastEmp] = firstName + " " + lastName;
  empPhone[lastEmp] = phoneExt;
  addToTrie(firstName + lastName, lastEmp);
  addToTrie(lastName + firstName, lastEmp);
}

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 <= 9999) break;
}

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

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 28, 2000
Revised: April 26, 2000

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