This question has already been answered:

The task

// Телефонная книга var phoneBook = {}; /** * @param {String} command * @returns {*} - результат зависит от команды */ module.exports = function (command) { }; 

Here, in the function, you need to write the code, which, when writing the above commands, either writes, deletes or shows the name and number. That's just not clear the task itself, how and what to build on, please tell me

Reported as a duplicate at Grundy. javascript Jan 2 '18 at 13:25 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

2 answers 2

For each operation, you can write your own function, for example for ADD

 function AddNumber(name,phone) 

and control function

 function phonebook(command) 

In this function, you need to call different functions depending on сommand . The command itself can be split into parts using the string.split ('') method. Depending on the first token (the first part), call different commands.

  • Thanks for the help) - AlexanderK
  • and functions for def. write operations in the control function? - AlexanderK
  • @AlexanderKaramushko each function is written by itself, but they will all be called from the manager - Dmitry Polyanin

Just use split , dividing the command into elements, and then analyze these elements separately.

If there is no time to read about split:
split ("one, two, three", ",") -> ["one", "two", "three"].
split ("12480282", "2") -> ["1", "480", "8"].
Split divides the given string into parts. Each part is separated from each other by a symbol that is specified in the second parameter.

  • I did, but how then, tell me? - AlexanderK
  • @ Alexander Karamushko, then everything is simple. If the first element of the array == "ADD", then in the second element of the array there will be a number (2 numbers? You can make a split ("the second element of the array", ",")). Then we just add this number (numbers?) To the book. - user263096 1:21 pm
  • If the first element == "REMOVE_PHONE", then in the second element of the array number. He too is split and put in a book. If the first element == "SHOW", then simply display the values ​​from the book. - user263096
  • I've done as you said, nothing at all, the element is added to the book, but does not want to be deleted - AlexanderK
  • @AlexanderKaramushko, first you need to check that everything is recorded correctly. If "SHOW" outputs everything normally, then you need to look for infection in the handler (handler) of "REMOVE_PHONE". Add your question and add your code to it, maybe we will find a problem. - user263096