Quick actions

cmd+k|ctrl+k

Navigation

Languages

arrayclass

Snippet info

Language

JavaScript

Visibility

public

Author

154073f14c170a35c7f05e029c7ae31aeae07c9a

Created

2020-06-02T15:54:01Z

Updated

2020-06-02T15:54:01Z

class myArray {
  constructor() {
    this.length = 0;
    this.data = {};
  }

  get(index) {
    return this.data[index];
  }

  push(item) {
    this.data[this.length] = item;
    this.length++;
    return this.length;
  }
  
  pop() {
    const lastItem = this.data[this.length - 1];
    delete this.data[this.length - 1];
    this.length--;
    return lastItem;
  }
  
  delete(index) {
    const lastItem = this.data[index];
    this.shiftItems(index);
    return lastItem;
  }

  shiftItems(index) {
    for (let i = index; i < this.length - 1; i++) {
      this.data[i] = this.data[i + 1];
    }
    delete this.data[this.length - 1];
    this.length--;
  }
}

const newArray = new myArray();

newArray.get(0);
newArray.push("eggs");
newArray.push("kell");
newArray.push("one");
console.log(newArray);
newArray.push("apple");
newArray.push(1);
console.log(newArray);
newArray.pop()
console.log(newArray);
newArray.delete(2);
console.log(newArray);
INFO