For day 2 of #100DaysOfCrackingTheCodingInterview, I dug in to implementing an array structure. Rather than just write the traditional push/pop/find methods, I decided to stretch myself and try to implement all of the methods available on JavaScript arrays.

The Code So Far

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325

class DynamicArray {
    constructor(capacity = 10) {
        this.length = 0;
        this.data = {};
        this.capacity = capacity
    }
    push(...values) {
        let num = values.length;
        if (num + this.length >= this.capacity) {
            this.capacity *= 2;
        }
        while (num > 0) {
            this.data[this.length++] = values[values.length - num];
            num--;
        }
        return this.length;
    }
    pop() {
        return this.data[--this.length];
    }
    unshift(...values) {
        let num = values.length;
        if (num + this.length >= this.capacity) {
            this.capacity *= 2;
        }
        let length = this.length - 1;
        let newArray = {};
        while (length >= 0) {
            newArray[length + values.length] = this.data[length--]
        }

        let index = 0;
        while (index < num) {
            newArray[index] = values[index++];
            this.length++;
        }
        this.data = newArray;
        return this.length;
    }
    shift() {
        if (this.length > 0) {
            let first = this.data[0];
            for (let i = 1; i < this.length; i++) {
                this.data[i - 1] = this.data[i];
            }
            delete this.data[--this.length];
            return first;
        }
        return undefined;
    }
    reverse() {
        for (let i = 0, j = this.length - 1; i < this.length / 2; i++ , j--) {
            let temp = this.data[i];
            this.data[i] = this.data[j];
            this.data[j] = temp;
        }
        return this.data;
    }
    *entries() {
        let next = 0;
        while (next < this.length) {
            yield [ next, this.data[next++]]
        }
    }
    *keys() {
        let next = 0;
        while (next < this.length) {
            yield next++;
        }
    }
    *values() {
        let next = 0;
        while (next < this.length) {
            yield  this.data[next++];
        }
    }
    every(callback, thisArgument) {
        let currentThis, index;
        if (this == null) {
            throw new TypeError("this is null or not defined")
        }
        // If callback isn't callable, throw error
        if (typeof callback !== 'function' && Object.prototype.toString.call(callback) !== '[object Function]') {
            throw new TypeError();
        }

        // Set the value of 'this' if it has been passed in
        if (arguments.length > 1) {
            currentThis = thisArgument;
        }

        index = 0;

        while (index < this.length) {
            let value;

            if (index in this.data) {
                let result;
                value = this.data[index];
                if (currentThis) {
                    result = callback(currentThis, value, index);
                } else {
                    result = callback(value, index);
                }
                if (!result) {
                    return false;
                }
            }
            index++;
        }
        return true;
    }
    some(callback, thisArgument) {
        let currentThis, index;
        if (this == null) {
            throw new TypeError("this is null or not defined")
        }
        // If callback isn't callable, throw error
        if (typeof callback !== 'function' && Object.prototype.toString.call(callback) !== '[object Function]') {
            throw new TypeError();
        }

        // Set the value of 'this' if it has been passed in
        if (arguments.length > 1) {
            currentThis = thisArgument;
        }

        index = 0;

        while (index < this.length) {
            let value;

            if (index in this.data) {
                let result;
                value = this.data[index];
                if (currentThis) {
                    result = callback(currentThis, value, index);
                } else {
                    result = callback(value, index);
                }
                if(result) {
                    return true;
                }
            }
            index++;
        }
        return false;
    };
    filter(callback, thisArgument) {
        let currentThis, index;
        if (this == null) {
            throw new TypeError("this is null or not defined")
        }
        // If callback isn't callable, throw error
        if (typeof callback !== 'function' && Object.prototype.toString.call(callback) !== '[object Function]') {
            throw new TypeError();
        }

        // Set the value of 'this' if it has been passed in
        if (arguments.length > 1) {
            currentThis = thisArgument;
        }

        index = 0;
        let newArr = []
        while (index < this.length) {
            let value;

            if (index in this.data) {
                let result;
                value = this.data[index];
                if (currentThis) {
                    result = callback(currentThis, value, index);
                } else {
                    result = callback(value, index);
                }
                if (result) {
                    newArr.push(value)
                }
            }
            index++;
        }
        return newArr;
    }
    includes(value, fromIndex = 0) {
        if (fromIndex >= this.length) {
            return false;
        }
        let index;
        if (fromIndex < 0) {
            index = this.length + fromIndex;
        } else {
            index = fromIndex;
        }
        while (index < this.length) {
            if (this.data[index++] === value) {
                return true;
            }
        }
        return false;
    }
    find(callback, thisArgument) {
        let currentThis, index;
        if (this == null) {
            throw new TypeError("this is null or not defined")
        }
        // If callback isn't callable, throw error
        if (typeof callback !== 'function' && Object.prototype.toString.call(callback) !== '[object Function]') {
            throw new TypeError();
        }

        // Set the value of 'this' if it has been passed in
        if (arguments.length > 1) {
            currentThis = thisArgument;
        }

        index = 0;
        while (index < this.length) {
            let value;

            if (index in this.data) {
                let result;
                value = this.data[index];
                if (currentThis) {
                    result = callback(currentThis, value, index);
                } else {
                    result = callback(value, index);
                }
                if (result) {
                    return value;
                }
            }
            index++;
        }
        return undefined
    }
    findIndex (callback, thisArgument) {
        if (this.length === 0) return -1;
        let currentThis, index;
        if (this == null) {
            throw new TypeError("this is null or not defined")
        }
        // If callback isn't callable, throw error
        if (typeof callback !== 'function' && Object.prototype.toString.call(callback) !== '[object Function]') {
            throw new TypeError();
        }

        // Set the value of 'this' if it has been passed in
        if (arguments.length > 1) {
            currentThis = thisArgument;
        }

        index = 0;
        let newArr = []
        while (index < this.length) {
            let value;

            if (index in this.data) {
                let result;
                value = this.data[index];
                if (currentThis) {
                    result = callback(currentThis, value, index);
                } else {
                    result = callback(value, index);
                }
                if (result) {
                    return index;
                }
            }
            index++;
        }
        return -1;
    }
    indexOf(value, fromIndex = 0) {
        if (fromIndex >= this.length) return -1;
        let index;
        if (fromIndex >= 0) {
            index = fromIndex;
        } else if (fromIndex < 0) {
            index = this.length + fromIndex;
        }
        while (index < this.length) {
            if (this.data[index] === value) {
                return index;
            }
            index++;
        }
        return -1;
    }
    lastIndexOf(value, fromIndex = this.length - 1) {
        let index;
        if (fromIndex < 0) {
            index = this.length + fromIndex;
        } else if (fromIndex >= this.length) {
            index = this.length - 1;
        } else {
            index = fromIndex;
        }
        if (index < 0) return -1;
        while (index >= 0) {
            if (this.data[index] === value) {
                return index;
            }
            index--;
        }
        return -1;
    }
    concat(...arrays) {
        if (arrays.length === 0) {
            return {...this.data}
        }
        let newArr = new DynamicArray();
        const values = [...this.values()];
        newArr.push(...values)
        for (let i = 0; i < arrays.length; i++) {
            if (arrays[i].length > 1) {
                newArr.push(...arrays[i])
            } else {
                newArr.push(arrays[i])
            }
        }
        return newArr;
    }
}

The Tests So Far

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const { DynamicArray } = require('./index.js');
const expect = require('chai').expect;

describe('DynamicArray', () => {
    describe('push', () => {
        it('should add a value to the end of the array', () => {
            let arr = new DynamicArray();
            arr.push(3, 4);
            const actual = arr;
            const expected = {
                length: 2,
                data: {0: 3, 1: 4},
                capacity: 10
            };
            expect(actual).to.deep.equal(expected);
        });
        it('should return the new length of the array', () => {
            let arr = new DynamicArray();
            const actualLength = arr.push(3, 4);
            const expectedLength = 2;
            expect(actualLength).to.equal(expectedLength);
        })
        it('should double the capacity if the length meets or exceeds capacity', () => {
            let arr = new DynamicArray(2);
            arr.push(2, 3);
            const actual = arr.capacity;
            const expected = 4
            expect(actual).to.equal(expected)
        })
    })
    describe('pop', () => {
        describe('should remove the last element from the list and return it', () => {
            it('should work for integers', () => {
                let arr = new DynamicArray();
                arr.push(1,2,3,4,5);
                const actual = arr.pop();
                const expected = 5;
                expect(actual).to.equal(expected)
            })
            it('should work for strings', () => {
                let arr = new DynamicArray();
                arr.push('One', 'for', 'the', 'money');
                const actual = arr.pop();
                const expected = 'money';
                expect(actual).to.equal(expected)
            })
            it('should work for objects', () => {
                let arr = new DynamicArray();
                arr.push({doe: 'a deer'}, {ray: 'a drop of golden sun'}, {me: 'a name I call myself'}, { fa: 'a long, long way to run'});
                const actual = arr.pop();
                const expected = { fa: 'a long, long way to run'}
                expect(actual).to.deep.equal(expected)
            })
        })
    })
    describe('shift', () => {
        describe('should remove the first element of the list and return it', () => {
            it('should work for integers', () => {
                let arr = new DynamicArray();
                arr.push(1,2,3,3,4);
                const actual = arr.shift();
                const expected = 1;
                expect(actual).to.equal(expected)
            })
            it('should work for strings', () => {
                let arr = new DynamicArray();
                arr.push('One', 'is', 'the', 'loneliest', 'number');
                const actual = arr.shift();
                const expected = 'One';
                expect(actual).to.equal(expected)
            })
            it('should work for objects', () => {
                let arr = new DynamicArray();
                arr.push({love: 'love me do'}, {you: 'know I love you'});
                const actual = arr.shift();
                const expected = { love: 'love me do' };
                expect(actual).to.deep.equal(expected)
            })
        })
    })
    describe('unshift', () => {
        it('should add values to the start of the array', () => {
            let arr = new DynamicArray();
            arr.push(8,9,10);
            arr.unshift(1,2);
            const actual = arr;
            const expected = {
                length: 5,
                capacity: 10,
                data: {
                    0: 1,
                    1: 2,
                    2: 8,
                    3: 9,
                    4: 10,
                }
            };
            expect(actual).to.deep.equal(expected);
        })
        it('should return the new length of the array', () => {
            let arr = new DynamicArray();
            arr.push(3);
            const actual = arr.unshift(7,8,9);
            const expected = 4;
            expect(actual).to.equal(expected)
        })
        it('should double the capacity if the length meets or exceeds capacity', () => {
            let arr = new DynamicArray();
            arr.unshift(1,2,3,4,5,6,7,8,9,10,11);
            const actual = arr.capacity;
            const expected = 20;
            expect(actual).to.equal(actual)
        })
    })
})