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. Herenameis 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
- 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.namefails. Dot notation never reads a variable.phoneBook.namelooks for a key spelledname. WithphoneBook = {"Ben": "555-0199"}andconst name = "Ben",phoneBook.nameisundefined, whilephoneBook[name]is"555-0199". - Keys are strings. The key
"Ben"and the variablenameholding"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 asphoneBook.name: a string in the brackets is the key itself. Leave the quotes out to use the variable. - Returning the key. Returning
namegives 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".
