Skip to content

Phonebook #16

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

Open
wants to merge 2 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
11 changes: 11 additions & 0 deletions js-core/homeworks/homework-13/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home work 13</title>
</head>
<body>

<script src="src/main.js"></script>
</body>
</html>
55 changes: 55 additions & 0 deletions js-core/homeworks/homework-13/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//Task1 (hw12 ---> task1)
class Http {
constructor() {
this.ctx = {
req: {
PORT: 'number',
url: 'string'
},
res: {
status: 'number',
message: 'string',
header: {
content_type: 'application/json'
}
}
},
this.next = () => {}
};

createServer(fn) {
this.createServerCallback = fn;
return this;
};
listen(PORT, host) {
console.log(`Server running on https://${host}:${PORT}`);
this.createServerCallback(this.ctx, this.next);
};
};

const server = new Http().createServer(function(ctx, next) {
console.log(ctx);
}).listen(3000, 'localhost');


//Task2

class DataBase {
constructor() {
this.idSetTimeout;
};

time() {
clearTimeout(this.idSetTimeout);
this.idSetTimeout = setTimeout(function(){
console.log('The web server is down');
}, 5000);
};

query() {
this.time();
};
};

const dataBase = new DataBase();
dataBase.query();
10 changes: 10 additions & 0 deletions js-core/homeworks/phonebook/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>phonebook</title>
<meta charset="utf-8" />
</head>
<body>
<script src="index.js"></script>
</body>
</html>
106 changes: 106 additions & 0 deletions js-core/homeworks/phonebook/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
class PhoneApp {
constructor() {
this.dataBase = [
{id:1, name:'Vasya', phone:'qweqwe'},
{id:6, name:'Vasya', phone:'qweqwe'},
{id:3, name:'Vasya', phone:'qweqwe'},
{id:8, name:'Vasya', phone:'qweqwe'},
{id: 10, name: 'Tom2', phone: '0995385'},
{id: 10, name: 'Anita', phone: '0995305385'},
{id: 12, name: 'Anita', phone: '9995305385'}
]
};

checkForNumbers(phone) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably in most cases there should be better name similar to

isPhoneNumberValid

meaning of "is" prefix at the beginning that function return boolean

let arrPhone = phone.split('');
let numberArrPhone = arrPhone.map((elem) => {
return parseInt(elem);
});
let validNumber = numberArrPhone.some((elem) => {
return isNaN(elem)
});
if(validNumber) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can just write that way

return validNumber

// console.error('Телефонный номер должен содержать только цифры');
return false;
} else {
//console.log('Телефонный номер вылидный');
return true;
};
};

transformNumber(phone) {
if(this.checkForNumbers(phone)){
let arrPhone = phone.split('');
arrPhone.splice(0, 0, '(');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets think can we apply regexp here, it would simplify that construction a lot

arrPhone.splice(4, 0, ')', ' ');
arrPhone.splice(8, 0, '-');
arrPhone.splice(11, 0, '-');
return arrPhone.join('');
} else {
return 'Телефонный номер должен содержать только цифры';
};
};

addUser(id, name, phone) {
let objUser = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create an additional class which would create such a user.

And if we go further maybe even phone-number validation should be made inside of User class.

Not sure if it the responsibility of phone-book to validate users.
The main responsibility of phone-book is to find users, update them and delete them

id,
name
};
if(this.checkForNumbers (phone)) {
objUser.phone = phone;
this.dataBase.push(objUser);
} else {
console.log('Введите коренктный номер')
};
};

removeUserByName(name) {
let indexDelete = this.dataBase.findIndex((elem) => {
return elem.name === name;
});
if(indexDelete === -1) {
console.log('Нет такого пользователя');
} else {
this.dataBase.splice(indexDelete, 1);
};
};

searchByName(value) {
let newArr = [];
this.dataBase.forEach((elem) => {
if(elem.name == value) {
newArr.push(elem)
};
});
newArr.forEach((elem) => {
console.log('Поиск по имени --->', elem);
});
};

changeUserData(initialUser, value) {
let arr = this.dataBase;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's hard to understand what does that function doing

arr.forEach((elem) =>{
for(let key in elem) {
if(elem[key] == initialUser) {
elem[key] = value;
};
};
});
};

sortingUsers(value) {
function compare(a, b) {
return a[value] > b[value];
};
let tmp = this.dataBase.sort(compare);
console.log('Сортировка пользователей по заданному свойству --->', tmp);
};
};
let phoneApp = new PhoneApp();
console.log('проверенный и преобразованый номер --->', phoneApp.transformNumber('0995305385'));
console.log('номер не прошедший проверку --->', phoneApp.transformNumber('099530)5385'));
phoneApp.addUser(3, 'Natalia', '0995305300');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's better to pass directly object with properties it would "self-documented-code"

phoneApp.removeUserByName('Tom2');
phoneApp.searchByName('Natalia');
phoneApp.changeUserData(3, 5);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure it easy to understand what 3 and 5 refers to.

I guess the public API(interface) should looks similar to that one

changeUser(userIdToChange, updatedUser) {
 
}

and inside of that method, you looking for the user with unique ID and than update it

phoneApp.sortingUsers('id');