Start learning free

LESSON 129 OF 671 · DICTIONARIES - CHALLENGES

Phone Book Lookup in JavaScript: 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

    JAVASCRIPT · DICTIONARIES - CHALLENGES

    Phone Book Lookup in JavaScript, explained

    Problem overview

    The method phoneBookLookup receives phoneBook, an object that maps names to phone numbers, and a name. It returns the phone number stored under that name. The name is always one of the keys, so there is no missing-key case to handle.

    With phoneBook = {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"} and name = "Ben", the answer is "555-0199". When two people share a number, as in {"Tom": "555-0111", "Tim": "555-0111"}, looking up "Tim" still returns "555-0111". Only keys have to be unique, not values.

    Approach: bracket notation

    A JavaScript object is a collection of key and value pairs, which is why programmers use it as a dictionary. To read a value you give the object a key, and it gives back the value stored under it. There are two ways to write that, and this lesson depends on knowing which one to use.

    • Dot notation, phoneBook.Ben, works when the key is written directly in the code. The text after the dot is the key's literal spelling.
    • Bracket notation, phoneBook[name], evaluates the expression between the brackets and uses the result as the key. Here name is a variable, so its current value, such as "Ben", is the key.

    Because the key arrives in a parameter, bracket notation is the right choice.

    The lookup is fast no matter how large the phone book is. The object finds the key directly, so a book with a thousand names costs about the same as a book with three. Scanning through an array of pairs until the right name turns up would be slower and needs a loop, which this lesson avoids.

    Step by step

    1. Return the value at name. One expression does everything: phoneBook[name], returned from the method.

    The solution in JavaScript

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

    Details worth knowing:

    • Why phoneBook.name fails. Dot notation never reads a variable. phoneBook.name looks for a key spelled name. With phoneBook = {"Ben": "555-0199"} and const name = "Ben", phoneBook.name is undefined, while phoneBook[name] is "555-0199".
    • Keys are strings. The key "Ben" and the variable name holding "Ben" are the same key. Capital letters matter, so "ben" would not find "Ben".
    • A missing key gives undefined. It doesn't throw an error. This lesson promises the key exists, but the next kind of dictionary lesson usually asks what to do when it doesn't.

    If a name might be missing, the result is undefined. With const book = {"Ana": "555-0142"}, the expression book["Zed"] is undefined, with no error. A program that uses that value as a phone number has to check for it first.

    Edge cases and common mistakes

    • A single entry. {"Max": "555-0100"} with "Max" returns "555-0100". Nothing changes with the size of the object.
    • Shared numbers. The lookup goes from key to value, so two keys with the same value don't interfere with each other.
    • Quotes around the variable. phoneBook["name"] is the same as phoneBook.name: a string in the brackets is the key itself. Leave the quotes out to use the variable.
    • Returning the key. Returning name gives back the name, not the number.

    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 JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free