|
| 1 | +class MyArray { |
| 2 | + constructor(){ |
| 3 | + this.lenght = 0; |
| 4 | + this.data = {} |
| 5 | + } |
| 6 | + |
| 7 | + getData(index) { |
| 8 | + return this.data[index] |
| 9 | + } |
| 10 | + |
| 11 | + push(item) { |
| 12 | + this.data[this.lenght] = item |
| 13 | + this.lenght++; |
| 14 | + return this.lenght; |
| 15 | + } |
| 16 | + |
| 17 | + pop(){ |
| 18 | + const lastItem = this.data[this.lenght]; |
| 19 | + this.lenght-- |
| 20 | + delete this.data[this.lenght] |
| 21 | + return lastItem; |
| 22 | + } |
| 23 | + |
| 24 | + delete(index) { |
| 25 | + const item = this.data[index] |
| 26 | + this.shiftItems(index) |
| 27 | + return item |
| 28 | + } |
| 29 | + |
| 30 | + shiftItems(index) { |
| 31 | + for(let i = index; i < this.lenght -1; i++){ |
| 32 | + this.data[i] = this.data[i+1] |
| 33 | + } |
| 34 | + delete this.data[this.lenght - 1] |
| 35 | + this.lenght-- |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +const newArray = new MyArray(); |
| 41 | + |
| 42 | +newArray.push('first') |
| 43 | +newArray.push('second') |
| 44 | +newArray.push('third') |
| 45 | +newArray.push('forth') |
| 46 | +newArray.push('fifth') |
| 47 | + |
| 48 | +console.log(newArray) |
| 49 | +/* { |
| 50 | + lenght: 5, |
| 51 | + data: { |
| 52 | + '0': 'first', |
| 53 | + '1': 'second', |
| 54 | + '2': 'third', |
| 55 | + '3': 'forth', |
| 56 | + '4': 'fifth' |
| 57 | + } |
| 58 | +}*/ |
| 59 | + |
| 60 | +newArray.pop() |
| 61 | +console.log(newArray) |
| 62 | +/**{ |
| 63 | + lenght: 4, |
| 64 | + data: { '0': 'first', '1': 'second', '2': 'third', '3': 'forth' } |
| 65 | +} */ |
| 66 | + |
| 67 | +newArray.delete(2) |
| 68 | +console.log(newArray) |
| 69 | +/**{ |
| 70 | + lenght: 3, |
| 71 | + data: { '0': 'first', '1': 'second', '2': 'forth' } |
| 72 | +} */ |
0 commit comments