Skip to content

Commit 46a7617

Browse files
Balashov NikitaOlegLustenko
authored andcommitted
fixed
1 parent d8e290d commit 46a7617

File tree

1 file changed

+37
-30
lines changed
  • js-core/homeworks/phoneApp/src

1 file changed

+37
-30
lines changed

js-core/homeworks/phoneApp/src/main.js

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,50 @@ function PhoneApp() {
22
this.storage = [];
33
}
44

5-
PhoneApp.prototype.checkNumber = function(numberToCheck) {
6-
const toStringThePredictNumber = numberToCheck.toString();
7-
const toArrayTheString = toStringThePredictNumber.split('');
5+
PhoneApp.prototype.isNumber = function(stringToCheck) {
6+
const toStringTheArgument = stringToCheck.toString();
7+
const toArrayTheString = toStringTheArgument.split('');
88
let flag;
99

10-
toArrayTheString.forEach(elem => {
10+
toArrayTheString.some(elem => {
1111
const toNumberElem = Number(elem);
1212
if(isNaN(toNumberElem)) {
1313
flag = false;
1414
}
1515
});
1616

17-
if(flag === false) {
18-
return flag;
19-
} else {
20-
return true;
21-
}
17+
return flag !== false;
2218
}
2319

24-
PhoneApp.prototype.sortNumber = function(numberToSort) {
25-
if(this.checkNumber(numberToSort)) {
26-
const toStringTheNumber = numberToSort.toString();
27-
const firstThreeNumbers = toStringTheNumber.slice(0, 3);
20+
PhoneApp.prototype.formatedPhoneNumber = function(numberToSort) {
21+
const toStringTheNumber = numberToSort.toString();
22+
const toArrayTheString = toStringTheNumber.split('');
23+
let rewritedArray = [];
24+
25+
toArrayTheString.some(elem => {
26+
if(elem !== '-') {
27+
rewritedArray.push(elem)
28+
}
29+
});
30+
31+
const joinRewritedArray = rewritedArray.join('')
32+
33+
if(this.isNumber(joinRewritedArray)) {
34+
const firstThreeNumbers = joinRewritedArray.slice(0, 3);
2835
const bracketThisFirstThreeNumbers = `(${firstThreeNumbers}) `;
2936

30-
const secondTwoNumbers = toStringTheNumber.slice(3, 5);
37+
const secondTwoNumbers = joinRewritedArray.slice(3, 5);
3138
const bracketThisSecondTwoNumbers = `${secondTwoNumbers}-`;
3239

33-
const thirdTwoNumbers = toStringTheNumber.slice(5, 7);
40+
const thirdTwoNumbers = joinRewritedArray.slice(5, 7);
3441
const bracketThisThirdTwoNumbers = `${thirdTwoNumbers}-`;
3542

36-
let lastNumbers = '';
37-
for(let i = 7; i < toStringTheNumber.length; i++) {
38-
const elem = toStringTheNumber[i];
39-
lastNumbers += elem;
40-
}
43+
let lastNumbers = joinRewritedArray.slice(7, joinRewritedArray.length);
4144

42-
const finalConstraction = bracketThisFirstThreeNumbers + bracketThisSecondTwoNumbers + bracketThisThirdTwoNumbers + lastNumbers;
45+
const finalConstraction = bracketThisFirstThreeNumbers +
46+
bracketThisSecondTwoNumbers +
47+
bracketThisThirdTwoNumbers +
48+
lastNumbers;
4349

4450
return finalConstraction;
4551
} else {
@@ -53,7 +59,7 @@ PhoneApp.prototype.add = function(name, number, surname = null, city = null, com
5359
id: idNumber,
5460
name: name,
5561
surname: surname,
56-
number: this.sortNumber(number),
62+
number: this.formatedPhoneNumber(number),
5763
city: city,
5864
company: company
5965
}
@@ -80,14 +86,14 @@ PhoneApp.prototype.removeByIndex = function(index) {
8086
let firstHalf = [];
8187
let lastHalf = [];
8288

83-
for(let i = 0; i < index; i++) {
84-
const elem = this.storage[i];
85-
firstHalf.push(elem);
86-
}
87-
for(let i = index + 1; i < this.storage.length; i++) {
88-
const elem = this.storage[i];
89-
lastHalf.push(elem);
90-
}
89+
this.storage.forEach((elem, i) => {
90+
if(i < index) {
91+
firstHalf.push(elem);
92+
}
93+
if(i < this.storage.length && i > index) {
94+
lastHalf.push(elem)
95+
}
96+
});
9197

9298
this.storage = [...firstHalf, ...lastHalf];
9399
}
@@ -109,3 +115,4 @@ console.log(test.filterByValue('name', 'Nikita'));
109115
console.log(test.filterByValue('name', 'Paul'));
110116
console.log(test.filterByValue('city', 'Kharkiv'));
111117
console.log(test.filterByValue('surname', 'Balashov'));
118+
test.isNumber('099-13-15-100')

0 commit comments

Comments
 (0)