Build a phone directory that parses first [middle] last, number lines and looks up numbers by last name

easyAbout 20 minutesUpdated CodingProAsked atGrubhubparsinghash-mapsetsstringsclassesapi-design
Not solved

Share this question

Problem

A support tool receives contact lines like "Ana Maria Lopez, 415-555-0134" and needs a small API to answer "which numbers do we have for the Lopez family?". Build it.

Line format. A line is a name, one comma, and a phone number.

  • The name is a first name, an optional middle name and a last name: 2 or 3 words separated by whitespace (spaces or tabs, any amount). A word is any run of characters that are not whitespace or a comma.
  • The number is exactly ddd-ddd-dddd, ten ASCII digits in groups of 3, 3 and 4 joined by hyphens.
  • Whitespace at the start or end of the line and around the comma is ignored.
  • Anything else is malformed: no comma or more than one, 1 or 4+ name words, or a number in another format.

class PhoneDirectory

  • new PhoneDirectory(lines = []) adds each line in order. Malformed lines, and lines that are not strings, are skipped, not thrown. A lines value that is not an array throws a TypeError.
  • skipped (a getter): a new array of the skipped lines, exactly as they were given, in order.
  • add(line) adds one line. It returns true if that person did not have this number yet, and false if they did (nothing changes then). It throws a TypeError if line is not a string and a SyntaxError if it is malformed. A thrown line is not added to skipped; only the constructor fills that list.
  • getNumbersByLastName(lastName) returns every number listed under that last name, for all people with it.
  • getNumbersByName(fullName) returns the numbers of one person. The full name must match all of the person's words, so "Ana Lopez" and "Ana Maria Lopez" are different people.

Both lookups:

  • ignore case and surrounding whitespace (" lOPEZ " finds Lopez), and getNumbersByName also ignores how much whitespace separates the words;
  • return numbers in the order they were first added, each number once, even if two people share it;
  • return a new empty array when nothing matches;
  • throw a TypeError for an argument that is not a string.
const directory = new PhoneDirectory([
  "Ana Maria Lopez, 415-555-0134",
  "Luis Lopez, 415-555-0199",
  "Grace Hopper, 212-555-0101",
  "Lopez, 415-555-0000",              // malformed: one name word
  "Ana Maria Lopez, 415-555-0134",    // already listed, no change
]);

directory.getNumbersByLastName("lopez");          // ["415-555-0134", "415-555-0199"]
directory.getNumbersByName("ana maria LOPEZ");    // ["415-555-0134"]
directory.getNumbersByName("Ana Lopez");          // []: a different person
directory.skipped;                                // ["Lopez, 415-555-0000"]

directory.add("  Grace   Brewster  Hopper ,212-555-0177 "); // true
directory.getNumbersByLastName("HOPPER");         // ["212-555-0101", "212-555-0177"]
directory.add("Grace Hopper 212-555-0101");        // throws SyntaxError: no comma

Ref. UR-1542 · © UIRound · uiround.com/questions/build-a-phone-directory-that-parses-first-middle-last-number-lines-and-looks-up-numbers-by-last-name

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment