Start learning free

LESSON 131 OF 289 · DICTIONARIES - CHALLENGES

Phone Book Lookup in TypeScript: Reading an Object by Key

Practice Read the full explanation

Given a phone book phoneBook, a dictionary from names to phone numbers, and a name, return the phone number of name.

Every name in the phone book is a key, and its value is that person's phone number, a string like "555-0142". The name you look up is always in the phone book.

 

Example 1:

Input: phoneBook = {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"}, name = "Ben"
Output: "555-0199"
Explanation: The key "Ben" maps to "555-0199".

Example 2:

Input: phoneBook = {"Max": "555-0100"}, name = "Max"
Output: "555-0100"
Explanation: The phone book has a single entry, and it is the one we look up.

Example 3:

Input: phoneBook = {"Tom": "555-0111", "Tim": "555-0111"}, name = "Tim"
Output: "555-0111"
Explanation: Tom and Tim share a phone number. We only need the value of the key "Tim".

 

Constraints:

  • phoneBook has at least one entry.
  • name is always a key of phoneBook.
  • Names are single words made of English letters, and they are all different. Two people can share a phone number.
  • Every phone number is a string of three digits, a dash and four digits, like "555-0142".
STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · DICTIONARIES - CHALLENGES

    Phone Book Lookup in TypeScript, explained

    Problem overview

    The method phoneBookLookup receives phoneBook, an object used as a dictionary from names to phone numbers, and a name. It returns the phone number stored under that name. The name is always a key of the phone book.

    With {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"} and name = "Ben", the answer is "555-0199". Two people can share a number: with {"Tom": "555-0111", "Tim": "555-0111"} and "Tim", the answer is "555-0111". Only the value under the requested key matters.

    Approach

    In TypeScript a plain object holds key and value pairs, so it works as a dictionary. The type { [key: string]: string } says that every key is a string and every value is a string. To read the value stored under a key, put the key in square brackets after the object.

    The key here is not written in the code. It sits in the variable name, and the number to return depends on what that variable holds. Square brackets evaluate the expression inside them, so phoneBook[name] looks up the key whose text is the current value of name.

    Step by step

    1. Read the value with brackets and return it. One line does the whole job: phoneBook[name] is the number, and return hands it back.

    The solution in TypeScript

    class Solution {
        phoneBookLookup(phoneBook: { [key: string]: string }, name: string): string {
            return phoneBook[name];
        }
    }
    

    The detail that matters is the difference between brackets and the dot:

    • Brackets use the variable's value. With name = "Ben", phoneBook[name] reads the key "Ben".
    • The dot uses a literal key. phoneBook.name always looks for a key spelled name. With {"Ana": "555-0142"} it gives undefined, because there is no such key, and the variable name is never consulted.
    • A missing key gives undefined. Reading phoneBook["Zed"] from that same object also gives undefined, with no error. This lesson promises that the name is always present, so the code doesn't need to handle that case.
    • Types are erased. The string return type is a promise to the compiler. At run time nothing checks that the value is a string.

    Complexity

    Time: O(1) on average. Reading a property from an object is a hash-style lookup, and it does not scan the entries one by one.

    Space: O(1). The method creates nothing new. It returns a value that is already stored in the object.

    Edge cases and common mistakes

    • Single entry. {"Max": "555-0100"} with "Max" returns "555-0100".
    • Shared numbers. Different keys may hold the same value, as with Tom and Tim. The lookup goes from the key to the value, so this causes no trouble.
    • Dot notation. phoneBook.name compiles without any error for this parameter type, but it returns undefined unless the object has a key spelled name. Use brackets.
    • Quotes around the variable. phoneBook["name"] is the dot version again: it asks for the literal key name.
    • Numbers are text. The values are strings such as "555-0142", with a dash, so the method returns them unchanged and does no arithmetic on them.
    • Case. Keys are compared exactly, so "ben" is not "Ben".

    Testing

    • {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"} with "Ben" gives "555-0199".
    • {"Max": "555-0100"} with "Max" gives "555-0100".
    • {"Tom": "555-0111", "Tim": "555-0111"} with "Tim" gives "555-0111".
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free