Исправленный вариант Александр3297
var phoneBook = {};
function re (command) {
// команда add
if (command.split(' ')[0] =='ADD' ){
var name = command.split(' ')[1];
var numb = (command.split(' ')[2]).split(',');
if(!phoneBook.hasOwnProperty(name)) {
phoneBook[name] = numb;
return phoneBook[name];
}
else {
phoneBook[name] = phoneBook[name].concat(numb);
return Object.keys(phoneBook) + ': ' + phoneBook[name];
}
}
// команда remove
else if (command.split(' ')[0] =='REMOVE_PHONE' ){
var numb = (command.split(' ')[1]);
for (i=0; i<Object.keys(phoneBook).length; i++){
var key = Object.keys(phoneBook)[i];
for (j=0;j<phoneBook[key].length; j++){
if(phoneBook[key][j] == numb) {
phoneBook[key].splice(j, 1);
return true;
}
}
return false;
}
}
// команда show
if (command.split(' ')[0] =='SHOW'){
var book = [];
for (i=0; i<(Object.keys(phoneBook)).length; i++){
var key = Object.keys(phoneBook)[i];
book[i] = [key + ': ' + phoneBook[key] + '\n'];
}
return book;
}
}
alert(re('ADD Ivan 223232'));
alert(re('ADD Ivan 555-10-01,555-10-03'));
alert(re('SHOW'));
alert (re('REMOVE_PHONE 555-10-01'));
alert (re('ADD Alex 544545')) ;
alert(re('SHOW'));