Quick actions

cmd+k|ctrl+k

Navigation

Languages

build an array

Snippet info

Language

JavaScript

Visibility

public

Author

bneuhausz

Created

2025-02-04T14:32:00.028012Z

Updated

2025-02-04T14:32:00.028012Z

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 item = this.data[index];
        this.shiftItems(index);
        return item;
    }
    
    shiftItems(index) {
        for (let i = index; i < this.length-1; i++) {
            this.data[i] = this.data[i+1];
        }
        this.pop();
    }
}

const newArray = new MyArray();
newArray.push('hi');
newArray.push('you');
newArray.push('!');
newArray.delete(0);
newArray.push('are');
newArray.push('nice');
newArray.delete(1);
console.log(newArray);














INFO