|
| 1 | +function PhoneApp() { |
| 2 | + this.storage = []; |
| 3 | +} |
| 4 | + |
| 5 | +PhoneApp.prototype.checkNumber = function(numberToCheck) { |
| 6 | + const toStringThePredictNumber = numberToCheck.toString(); |
| 7 | + const toArrayTheString = toStringThePredictNumber.split(''); |
| 8 | + let flag; |
| 9 | + |
| 10 | + toArrayTheString.forEach(elem => { |
| 11 | + const toNumberElem = Number(elem); |
| 12 | + if(isNaN(toNumberElem)) { |
| 13 | + flag = false; |
| 14 | + } |
| 15 | + }); |
| 16 | + |
| 17 | + if(flag === false) { |
| 18 | + return flag; |
| 19 | + } else { |
| 20 | + return true; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +PhoneApp.prototype.sortNumber = function(numberToSort) { |
| 25 | + if(this.checkNumber(numberToSort)) { |
| 26 | + const toStringTheNumber = numberToSort.toString(); |
| 27 | + const firstThreeNumbers = toStringTheNumber.slice(0, 3); |
| 28 | + const bracketThisFirstThreeNumbers = `(${firstThreeNumbers}) `; |
| 29 | + |
| 30 | + const secondTwoNumbers = toStringTheNumber.slice(3, 5); |
| 31 | + const bracketThisSecondTwoNumbers = `${secondTwoNumbers}-`; |
| 32 | + |
| 33 | + const thirdTwoNumbers = toStringTheNumber.slice(5, 7); |
| 34 | + const bracketThisThirdTwoNumbers = `${thirdTwoNumbers}-`; |
| 35 | + |
| 36 | + let lastNumbers = ''; |
| 37 | + for(let i = 7; i < toStringTheNumber.length; i++) { |
| 38 | + const elem = toStringTheNumber[i]; |
| 39 | + lastNumbers += elem; |
| 40 | + } |
| 41 | + |
| 42 | + const finalConstraction = bracketThisFirstThreeNumbers + bracketThisSecondTwoNumbers + bracketThisThirdTwoNumbers + lastNumbers; |
| 43 | + |
| 44 | + return finalConstraction; |
| 45 | + } else { |
| 46 | + return console.log('EROR you had typed incorect number') |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +PhoneApp.prototype.add = function(name, number, surname = null, city = null, company = null) { |
| 51 | + const idNumber = this.storage.length + 1; |
| 52 | + const contact = { |
| 53 | + id: idNumber, |
| 54 | + name: name, |
| 55 | + surname: surname, |
| 56 | + number: this.sortNumber(number), |
| 57 | + city: city, |
| 58 | + company: company |
| 59 | + } |
| 60 | + this.storage.push(contact); |
| 61 | +} |
| 62 | + |
| 63 | +PhoneApp.prototype.filterByValue = function(key, value) { |
| 64 | + return this.storage.filter((elem, index) => { |
| 65 | + return elem[key] === value; |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +PhoneApp.prototype.sortByKey = function(key) { |
| 70 | + const sortFunction = function(a, b) { |
| 71 | + if(a[key] > b[key]) return 1; |
| 72 | + if(a[key] < b[key]) return -1; |
| 73 | + } |
| 74 | + |
| 75 | + let copyStorage = [...this.storage] |
| 76 | + return copyStorage.sort(sortFunction); |
| 77 | +} |
| 78 | + |
| 79 | +PhoneApp.prototype.removeByIndex = function(index) { |
| 80 | + let firstHalf = []; |
| 81 | + let lastHalf = []; |
| 82 | + |
| 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 | + } |
| 91 | + |
| 92 | + this.storage = [...firstHalf, ...lastHalf]; |
| 93 | +} |
| 94 | + |
| 95 | +PhoneApp.prototype.changeValueByIndex = function(index, key, value) { |
| 96 | + const elem = this.storage[index]; |
| 97 | + elem[key] = value; |
| 98 | +} |
| 99 | + |
| 100 | +const test = new PhoneApp(); |
| 101 | + |
| 102 | +test.add('Nikita', '1234567890', 'Balashov', 'Kharkiv'); |
| 103 | +test.add('Nikita', '0987655321', 'Smith'); |
| 104 | +test.add('Paul', '0893127823', 'Smith', 'Kharkiv'); |
| 105 | +test.add('Paul', '0897612374', 'Balashov'); |
| 106 | + |
| 107 | +console.log(test.storage); |
| 108 | +console.log(test.filterByValue('name', 'Nikita')); |
| 109 | +console.log(test.filterByValue('name', 'Paul')); |
| 110 | +console.log(test.filterByValue('city', 'Kharkiv')); |
| 111 | +console.log(test.filterByValue('surname', 'Balashov')); |
0 commit comments