Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions Call Book/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Phone Book</title>
</head>
<body>
<script type="text/javascript" src="js.js">

</script>
</body>
</html>
133 changes: 133 additions & 0 deletions Call Book/js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@




// Приложение телефонный справочник
//
// Создайте функцию конструктор.
// У данной функции должны быть методы:
//
// 1 Преобразование телефонного номера из формата 0993378130 в (099) 33-78-130++
// 2 Проверка, что телефонный номер содержит только числа ++
// 3 Добавление пользователей в справочник ++
// 4 Удаление пользователя по имени, фамилии ++
// 5 Поиск пользователей по имени - отображает всех пользователей с одинаковым именем ++
// 6 Изменение имени, фамилии, телефонного номера у выбраного пользователя ++
// 7 Сортировка пользователей по номеру телефона, фамилии, имени и тд, по любому из свойств пользователя++
// 8 Фильтр по указанному свойству++





function PhoneApp() {
this.dataBase = [
{id:1, name:'Vasya',thername: "Ivanov", numb: `(099)33-78-130`},
{id:2, name:'Dima', thername: "Ivanov", numb: `(099)33-78-130`},
{id:3, name:'Grisha', thername: "Ivanov", numb: `(099)33-78-130`},
{id:4, name:'Jora', thername: "Ivanov", numb: `(099)33-78-130`},
{id:5, name:'Jora', thername: "Ivanov", numb: `(099)33-78-130`},
]
}

PhoneApp.prototype.editUser = function(name,thername,numb) {
// проверяем длину номера
if (numb.length > 10) {
console.error(`make correct numb`);
}
// проверяем содержимое
for (var i = 0; i < numb.length; i++) {
var newNumb = +numb[i];
if (typeof newNumb != `number` || typeof newNumb == `NaN`) {
console.error(`not correct numb`);
continue;
}
}
// формируем корректный формат
let firstThreeNumb = `${numb[0]}${numb[1]}${numb[2]}`;
let secondTwoNumb = `${numb[3]}${numb[4]}`;
let therdTwoNumb = `${numb[5]}${numb[6]}`;
let fourthThreeNumb = `${numb[7]}${numb[8]}${numb[9]}`;
//формируем ID по длине массива + 1
let correctId = this.dataBase.length+1;

this.dataBase.push({id:correctId,name:name,thername:thername,numb:`(${firstThreeNumb})${secondTwoNumb}-${therdTwoNumb}-${fourthThreeNumb}`})
Copy link
Member

Choose a reason for hiding this comment

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

тут оччень длинная строка, предлагаю сделать отдельный конструктор для создания пользователя

}


PhoneApp.prototype.usersRemoving = function(username) {
this.dataBase.forEach((value, index, arr)=> {
if (value.name === username) {
delete this.dataBase[index];
Copy link
Member

Choose a reason for hiding this comment

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

будет дыра в массиве,

использования метода .filter - тут подойдет лучше


}else if(value.thername === username){
delete this.dataBase[index];
}
})
}

PhoneApp.prototype.searchUsers = function(username) {
let allUsers = [];
this.dataBase.forEach((value, index, arr)=> {
Copy link
Member

Choose a reason for hiding this comment

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

тоже подойдет лучше метод filter


if (value.name === username) {
allUsers.push(this.dataBase[index]);

}
})
console.log(allUsers);
}

PhoneApp.prototype.changeUsers = function(userId, name, thername, numb) {
// проверяем длину номера
if (numb.length > 10) {
console.error(`make correct numb`);
}
// проверяем содержимое
for (var i = 0; i < numb.length; i++) {
var newNumb = +numb[i];
if (typeof newNumb != `number` || typeof newNumb == `NaN`) {
console.error(`not correct numb`);
continue;
}
}
// формируем корректный формат
let firstThreeNumb = `${numb[0]}${numb[1]}${numb[2]}`;
let secondTwoNumb = `${numb[3]}${numb[4]}`;
let therdTwoNumb = `${numb[5]}${numb[6]}`;
let fourthThreeNumb = `${numb[7]}${numb[8]}${numb[9]}`;

this.dataBase.forEach((value, index, arr)=> {
let correctId = index + 1;
if (value.id == userId) {
this.dataBase[index] = {id:correctId,name:name,thername:thername,numb:`(${firstThreeNumb})${secondTwoNumb}-${therdTwoNumb}-${fourthThreeNumb}`};
Copy link
Member

Choose a reason for hiding this comment

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

{id:correctId,name:name,thername:thername,numb:`(${firstThreeNumb})${secondTwoNumb}-${therdTwoNumb}-${fourthThreeNumb}`};

это очень длинная строка, легче это называть в духе "makeUser" или что то такое

она даже не очень помещается в экран

image

};
});
}

PhoneApp.prototype.sortUsers = function(property) {

console.log(property);
var newArray = this.dataBase.sort((a, b) => {
return a[property] == b[property] ? 0 : a[property] < b[property] ? -1 : 1;})


console.log(newArray);
}
PhoneApp.prototype.filterUsers = function(property) {
let filteredArray = []
this.dataBase.filter((value,index,arr)=>{
// console.log(value[property]);
filteredArray.push(`${property}: ${value[property]}`);
})
console.log(filteredArray);
}

const myApp = new PhoneApp();
myApp.editUser(`Petiya`,`Petrov`, `0993378130`)
myApp.editUser(`Alex`,`Petrov`, `0093378130`)
// myApp.filterUsers(`name`)



console.log(myApp);