Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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/contact-list/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 1</title>
</head>
<body>

<script src="src/main.js"></script>
</body>
</html>
151 changes: 151 additions & 0 deletions js-core/homeworks/contact-list/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
class PhoneApp{
constructor(){
this.database = [];
}

addUser (options) {
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 method addUser accepts 'user' as param

let user = {};
user.id = this.database.length + 1;

if (options.name) {
user.name = options.name;
}
if (options.phone) {
Copy link
Member

Choose a reason for hiding this comment

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

please omit code duplication, there the same code at line 14

if (!this.checkPhoneNumber(options.phone)) {
user.phone = options.phone;
} else {
console.log(
"не удалось сохранить номер телефона, Телефон должен состоять только из цифр"
);
}
}
if (options.homePhone) {
if (!this.checkPhoneNumber(options.homePhone)) {
user.homePhone = options.homePhone;
} else {
console.log(
"не удалось сохранить номер телефона, Телефон должен состоять только из цифр"
);
}
}
this.database.push(user);
};
checkPhoneNumber(phone){isNaN(+phone)};
Copy link
Member

Choose a reason for hiding this comment

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

please make multi-line


checkAndFormatPhoneNumber(phone) {
if (!isNaN(+phone)) {
let phoneCode = phone
.split("")
.splice(0, 3)
.join("");
let resultPhone =
Copy link
Member

Choose a reason for hiding this comment

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

what about regExp ?

"(" +
phoneCode +
") " +
phone.slice(3, 5) +
"-" +
phone.slice(5, 7) +
"-" +
phone.slice(7);
return resultPhone;
}
};

deleteUser(id) {
this.database.forEach((elem, index) => {
Copy link
Member

Choose a reason for hiding this comment

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

such case would be much better

this.database = this.database.filter( ... )

if (elem.id == id) {
this.database.splice(index, 1);
}
});
this.database.splice();
Copy link
Member

Choose a reason for hiding this comment

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

a lot of splices here

};
searchUserByName(name) {
return this.database.reduce(function(newElem, elem) {
Copy link
Member

Choose a reason for hiding this comment

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

use Array.prototype.filter is fine here and much better than reduce

if (elem.name == name) {
newElem.push(elem);
}
return newElem;
}, []);
};
editUser(id, options) {
this.database.map(elem => {
if (elem.id == id) {
if (options.name) {
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible use don't have such property?

elem.name = options.name;
}
if (options.phone) {
Copy link
Member

Choose a reason for hiding this comment

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

same about that property

elem.phone = options.phone;
}
if (options.homePhone) {
elem.homePhone = options.homePhone;
}
}
});
};
filterUser(param) {
return this.database.filter(elem => {
if (elem[param]) {
return elem;
}
});
};
sortUser(param, direction) {
return this.database.sort((a, b) => {
if (direction) {
if (direction == "big") {
Copy link
Member

Choose a reason for hiding this comment

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

what does small/big refer to ? :)

return a[param] < b[param];
}
if (direction == "small") {
return a[param] > b[param];
}
} else {
return a.id > b.id;
}
});
};
}

class User{
constructor(options){
this.name = options.name
this.phone = options.phone
this.homePhone = options.homePhone
}
}

let vasya = new User({
name: "Vasya",
phone: "123456789",
homePhone: "11111"
})
let petja = new User({ name: "Petja", phone: "123456798" })
let brigitte = new User({ name: "Brigitte", phone: "123457689" })
let tracer = new User({ name: "Tracer", phone: "123546789" })
let anduin = new User({
name: "Anduin",
phone: "113456789",
homePhone: "535353"
})
let torgrim = new User({ name: "Torgrim", phone: "321456789" })
let anduin2 = new User({
name: "Anduin",
phone: "113451189",
homePhone: "222222"
})

const myPhoneApp = new PhoneApp();
myPhoneApp.addUser(vasya);
Copy link
Member

Choose a reason for hiding this comment

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

a pretty interesting decision to create user outside of phoneApp.

But it will be even better to make additional "mental-layer" to create such users. That layer would validate and than send user to phoneApp the main goal of such approach to make sure DATA(users) already validated and we just want to operate with them, without any validation inside internal methods of PhoneApp

myPhoneApp.addUser(petja);
myPhoneApp.addUser(brigitte);
myPhoneApp.addUser(tracer);
myPhoneApp.addUser(anduin);
myPhoneApp.addUser(torgrim);
myPhoneApp.addUser(anduin2);

console.log(myPhoneApp);
console.log(myPhoneApp.searchUserByName("Anduin"));
myPhoneApp.editUser(2, { name: "Voljin" });
myPhoneApp.editUser(4, { homePhone: "159357" });
console.log(myPhoneApp.filterUser("homePhone"));
console.log(myPhoneApp.sortUser("phone", "big"));
// console.log(myPhoneApp.checkAndFormatPhoneNumber("0993378130"));
Copy link
Member

Choose a reason for hiding this comment

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

let clean up it a bit, please remove unused parts of the code