Start learning free

LESSON 132 OF 671 · DICTIONARIES - CHALLENGES

Add Contact in JavaScript: Adding a Key Only If New

Practice Read the full explanation

You get a phone book phoneBook, a dictionary from names to phone numbers. You also get a name and a phone number.

Add name with its number to the phone book, and return the phone book.

Only add name when it isn't in the phone book yet. A name that is already there keeps its old number. The order of the entries doesn't matter.

 

Example 1:

Input: phoneBook = {"Ana": "555-0142", "Ben": "555-0199"}, name = "Cara", number = "555-0123"
Output: {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"}
Explanation: "Cara" isn't in the phone book yet, so she is added.

Example 2:

Input: phoneBook = {"Ana": "555-0142", "Ben": "555-0199"}, name = "Ben", number = "555-0100"
Output: {"Ana": "555-0142", "Ben": "555-0199"}
Explanation: "Ben" is already in the phone book. He keeps his old number, "555-0199".

Example 3:

Input: phoneBook = {}, name = "Max", number = "555-0100"
Output: {"Max": "555-0100"}
Explanation: The phone book was empty. Now it has one contact.

 

Constraints:

  • phoneBook can be empty.
  • Names are single words made of English letters.
  • Every phone number has three digits, a dash and four digits, like "555-0142".
  • Two people can share a phone number.
STEP 1 OF 3
    Ctrl + Enter to check

    JAVASCRIPT · DICTIONARIES - CHALLENGES

    Add Contact in JavaScript, explained

    Problem overview

    The method addContact receives a phone book phoneBook, an object that maps names to phone numbers, a name and a phone number. It adds name with its number to the phone book and returns the phone book. The add happens only when name is not in the phone book yet. A name that is already there keeps its old number. The order of the entries does not matter.

    The constraints say the phone book can be empty, names are single words made of English letters, and numbers look like "555-0142". Two people can share a number.

    The examples:

    • Adding "Cara" with "555-0123" to a book that has Ana and Ben gives a book with all three, since Cara is new.
    • Adding "Ben" with "555-0100" changes nothing. Ben keeps "555-0199".
    • Adding "Max" to an empty book gives a book with one contact.

    Approach

    The key question is "is this name already a key?" The in operator answers it: name in phoneBook is true when the object has that key. We want to add only in the opposite case, so we negate the check.

    The reason to check at all is that assigning to an existing key replaces its value. A plain phoneBook[name] = number would overwrite Ben's old number with the new one, which this task forbids. The if guards the assignment.

    Step by step

    1. Write an if that checks that name is not a key of phoneBook: put ! in front of the whole in check, wrapped in parentheses. Write the closing brace right away.
    2. Inside the if, store number under the key name. Assigning to a key that is not there yet adds it, with its value.
    3. After the closing brace, return phoneBook. It either has the new contact, or it is as it was.

    The solution in JavaScript

    class Solution {
        addContact(phoneBook, name, number) {
            if (!(name in phoneBook)) {
                phoneBook[name] = number;
            }
            return phoneBook;
        }
    }
    

    Details worth knowing

    The parentheses matter. Without them, !name in phoneBook applies the ! to the name alone, which gives false, and then asks whether the key "false" exists. That is false unless the book has a key named "false", so the if almost never runs and no contact is added.

    The square brackets use the variable's value as the key. phoneBook[name] with name = "Cara" means the key "Cara", whereas phoneBook.name would mean a key literally called "name".

    The method changes the object it was given and returns that same object.

    Testing

    Running the method on the three examples and printing each result with JSON.stringify gives:

    {"Ana":"555-0142","Ben":"555-0199","Cara":"555-0123"}
    {"Ana":"555-0142","Ben":"555-0199"}
    {"Max":"555-0100"}
    

    Edge cases and common mistakes

    • Assigning without the check. The line phoneBook["Ben"] = "555-0100" replaces Ben's number, and the result shows {"Ben":"555-0100"} instead of the old number.
    • Forgetting the parentheses. See above: the ! binds to the name, not to the whole check, and new contacts are never added.
    • Checking the values. The in operator looks at keys only. Two people may share a number, so a number being in the book says nothing about the name.
    • Returning inside the if. The return belongs after the block, so it also runs when the name was already there.
    Keep going with JavaScript500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free