Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Барабанов Григорий #120

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';

const phoneBook = require('./phone-book');

// Эти записи добавятся, вернется `true`
Expand All @@ -16,10 +15,12 @@ phoneBook.add('5555550055');
// Обновление ранее добавленных записей
phoneBook.update('5551110011', 'Алексей', '[email protected]');
phoneBook.update('5553330033', 'Валерий');
phoneBook.update('5553330033');

// В следующих примерах вернутся все записи
console.info(phoneBook.find('*'));
console.info(phoneBook.find('555'));
console.info(phoneBook.find('Григорий'));
// В обоих случаях вывод будет следующий
// [
// 'Алексей, +7 (555) 111-00-11, [email protected]',
Expand Down
70 changes: 68 additions & 2 deletions phone-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
* Сделано задание на звездочку
* Реализован метод importFromCsv
*/
const isStar = true;
const isStar = false;

/**
* Телефонная книга
*/
let phoneBook;
let phoneBook = [];
let phoneNotes = [];
let noteEmail = '';

/**
* Добавление записи в телефонную книгу
Expand All @@ -19,7 +21,23 @@ let phoneBook;
* @returns {Boolean}
*/
function add(phone, name, email) {
let isNoteNew = true;
phoneBook.forEach(function (note) {
if (note.name === name || note.phone === phone) {
isNoteNew = false;
}
});
if (name !== undefined && isNoteNew && phone.length === 10 && !isNaN(phone)) {
let newName = {};
newName.name = name;
newName.phone = phone;
newName.email = email;
phoneBook.push(newName);

return (true);
}

return (false);
}

/**
Expand All @@ -30,7 +48,18 @@ function add(phone, name, email) {
* @returns {Boolean}
*/
function update(phone, name, email) {
let isUpdated = false;
for (let i = 0; i < phoneBook.length; i++) {
if (phoneBook[i].phone === phone && name !== undefined) {
phoneBook[i].name = name;
phoneBook[i].email = email;
isUpdated = true;

return (isUpdated);
}
}

return (isUpdated);
}

/**
Expand All @@ -39,7 +68,15 @@ function update(phone, name, email) {
* @returns {Number}
*/
function findAndRemove(query) {
let deletedNotesCount = 0;
for (let i = 0; i < phoneBook.length; i++) {
if (isNoteFound(query, i)) {
phoneNotes.splice(i, 1);
deletedNotesCount++;
}
}

return deletedNotesCount;
}

/**
Expand All @@ -48,7 +85,36 @@ function findAndRemove(query) {
* @returns {String[]}
*/
function find(query) {
phoneNotes = [];
if (query.length === 0 || query === undefined) {

return;
}
for (let i = 0; i < phoneBook.length; i++) {
if (isNoteFound(query, i)) {
phoneNotes.push(phoneBook[i].name + ', +7 (' + phoneBook[i].phone.substr(0, 3) + ') ' +
phoneBook[i].phone.substr(3, 3) + '-' + phoneBook[i].phone.substr(6, 2) + '-' +
phoneBook[i].phone.substr(8, 2) + noteEmail);
}
}

return phoneNotes.sort();
}

function isNoteFound(query, i) {
noteEmail = '';
if (phoneBook[i].email === undefined) {
noteEmail = '';
} else {
noteEmail = ', ' + phoneBook[i].email;
}
if (phoneBook[i].name.indexOf(query) >= 0 || phoneBook[i].phone.indexOf(query) >= 0 ||
noteEmail.indexOf(query) >= 0 || query === '*') {

return true;
}

return false;
}

/**
Expand Down