Skip to content

Commit aeb0e4f

Browse files
committedJun 7, 2018
some eslint fixes
1 parent b6d1808 commit aeb0e4f

File tree

17 files changed

+123
-128
lines changed

17 files changed

+123
-128
lines changed
 

‎src/data-structures/hash-maps/hash-map-1.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Naïve HashMap implementation
33
*/
44
class HashMap {
5-
65
/**
76
* Initialize array that holds the values. Default is size 1,000
87
* @param {number} initialCapacity
@@ -52,6 +51,7 @@ class HashMap {
5251

5352
// Usage:
5453
const assert = require('assert');
54+
5555
const hashMap = new HashMap();
5656

5757
hashMap.set('cat', 2);
@@ -68,4 +68,4 @@ console.log(hashMap.buckets);
6868
assert.equal(hashMap.get('art'), 8); // this one is ok
6969
assert.equal(hashMap.get('cat'), 8); // got overwritten by art 😱
7070
assert.equal(hashMap.get('rat'), 8); // got overwritten by art 😱
71-
assert.equal(hashMap.get('dog'), 8); // got overwritten by art 😱
71+
assert.equal(hashMap.get('dog'), 8); // got overwritten by art 😱

‎src/data-structures/hash-maps/hash-map-2.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Hash Map data structure implementation
33
*/
44
class HashMap {
5-
65
/**
76
* Initialize array that holds the values. Default is size 1,000
87
* @param {number} initialCapacity
@@ -19,11 +18,11 @@ class HashMap {
1918
*/
2019
set(key, value) {
2120
const bucketIndex = this.getIndex(key);
22-
if(this.buckets[bucketIndex]) {
23-
this.buckets[bucketIndex].push({key, value});
24-
if(this.buckets[bucketIndex].length > 1) { this.collisions++; }
21+
if (this.buckets[bucketIndex]) {
22+
this.buckets[bucketIndex].push({ key, value });
23+
if (this.buckets[bucketIndex].length > 1) { this.collisions++; }
2524
} else {
26-
this.buckets[bucketIndex] = [{key, value}];
25+
this.buckets[bucketIndex] = [{ key, value }];
2726
}
2827
return this;
2928
}
@@ -36,8 +35,8 @@ class HashMap {
3635
const bucketIndex = this.getIndex(key);
3736
for (let arrayIndex = 0; arrayIndex < this.buckets[bucketIndex].length; arrayIndex++) {
3837
const entry = this.buckets[bucketIndex][arrayIndex];
39-
if(entry.key === key) {
40-
return entry.value
38+
if (entry.key === key) {
39+
return entry.value;
4140
}
4241
}
4342
}
@@ -72,6 +71,7 @@ class HashMap {
7271

7372
// Usage:
7473
const assert = require('assert');
74+
7575
const hashMap = new HashMap();
7676

7777
hashMap.set('cat', 2);
@@ -125,4 +125,4 @@ hashMapSize100.set('dog', 1);
125125
hashMapSize100.set('art', 8);
126126

127127
console.log('collisions: ', hashMapSize100.collisions);
128-
console.log('hashMapSize100\n', hashMapSize100.buckets);
128+
console.log('hashMapSize100\n', hashMapSize100.buckets);

‎src/data-structures/hash-maps/hash-map-3.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* @author Adrian Mejia <me AT adrianmejia.com>
44
*/
55
class HashMap {
6-
76
/**
87
* Initialize array that holds the values. Default is size 1,000
98
* @param {number} initialCapacity initial size of the array
@@ -50,23 +49,23 @@ class HashMap {
5049
* @param {any} value
5150
*/
5251
set(key, value) {
53-
const {bucketIndex, entryIndex} = this._getIndexes(key);
52+
const { bucketIndex, entryIndex } = this._getIndexes(key);
5453

55-
if(entryIndex === undefined) {
54+
if (entryIndex === undefined) {
5655
// initialize array and save key/value
57-
const keyIndex = this.keys.push({content: key}) - 1; // keep track of the key index
56+
const keyIndex = this.keys.push({ content: key }) - 1; // keep track of the key index
5857
this.buckets[bucketIndex] = this.buckets[bucketIndex] || [];
59-
this.buckets[bucketIndex].push({key, value, keyIndex});
58+
this.buckets[bucketIndex].push({ key, value, keyIndex });
6059
this.size++;
6160
// Optional: keep count of collisions
62-
if(this.buckets[bucketIndex].length > 1) { this.collisions++; }
61+
if (this.buckets[bucketIndex].length > 1) { this.collisions++; }
6362
} else {
6463
// override existing value
6564
this.buckets[bucketIndex][entryIndex].value = value;
6665
}
6766

6867
// check if a rehash is due
69-
if(this.loadFactor > 0 && this.getLoadFactor() > this.loadFactor) {
68+
if (this.loadFactor > 0 && this.getLoadFactor() > this.loadFactor) {
7069
this.rehash(this.buckets.length * 2);
7170
}
7271

@@ -79,9 +78,9 @@ class HashMap {
7978
* @param {any} key
8079
*/
8180
get(key) {
82-
const {bucketIndex, entryIndex} = this._getIndexes(key);
81+
const { bucketIndex, entryIndex } = this._getIndexes(key);
8382

84-
if(entryIndex === undefined) {
83+
if (entryIndex === undefined) {
8584
return;
8685
}
8786

@@ -107,22 +106,22 @@ class HashMap {
107106

108107
for (let entryIndex = 0; entryIndex < values.length; entryIndex++) {
109108
const entry = values[entryIndex];
110-
if(entry.key === key) {
111-
return {bucketIndex, entryIndex};
109+
if (entry.key === key) {
110+
return { bucketIndex, entryIndex };
112111
}
113112
}
114113

115-
return {bucketIndex};
114+
return { bucketIndex };
116115
}
117116

118117
/**
119118
* Returns true if an element in the Map object existed and has been removed, or false if the element does not exist.
120119
* @param {any} key
121120
*/
122121
delete(key) {
123-
const {bucketIndex, entryIndex, keyIndex} = this._getIndexes(key);
122+
const { bucketIndex, entryIndex, keyIndex } = this._getIndexes(key);
124123

125-
if(entryIndex === undefined) {
124+
if (entryIndex === undefined) {
126125
return false;
127126
}
128127

@@ -140,8 +139,8 @@ class HashMap {
140139
rehash(newCapacity) {
141140
const newMap = new HashMap(newCapacity);
142141

143-
this.keys.forEach(key => {
144-
if(key) {
142+
this.keys.forEach((key) => {
143+
if (key) {
145144
newMap.set(key.content, this.get(key.content));
146145
}
147146
});
@@ -167,6 +166,7 @@ const hashMap = new HashMap(1);
167166
// const hashMap = new Map();
168167

169168
const assert = require('assert');
169+
170170
assert.equal(hashMap.size, 0);
171171
hashMap.set('cat', 2);
172172
assert.equal(hashMap.size, 1);
@@ -213,32 +213,32 @@ assert.equal(hashMap.getLoadFactor(), 5);
213213
hashMap.rehash(1000);
214214
console.log(hashMap.collisions);
215215
console.log(hashMap.buckets);
216-
assert.equal(hashMap.getLoadFactor(), 5/1000);
216+
assert.equal(hashMap.getLoadFactor(), 5 / 1000);
217217

218218
// automatic rehashing based on loadFactor
219219
const dynamicMap = new HashMap(2, 0.75);
220220

221221
dynamicMap.set('uno', 1);
222222
assert.equal(dynamicMap.buckets.length, 2);
223-
assert.equal(dynamicMap.getLoadFactor(), 1/2);
223+
assert.equal(dynamicMap.getLoadFactor(), 1 / 2);
224224
console.log(hashMap.collisions);
225225

226226
dynamicMap.set('dos', 2);
227227
assert.equal(dynamicMap.buckets.length, 4); // <-- rehash took place
228-
assert.equal(dynamicMap.getLoadFactor(), 1/2);
228+
assert.equal(dynamicMap.getLoadFactor(), 1 / 2);
229229
console.log(hashMap.collisions);
230230

231231
dynamicMap.set('tres', 3);
232232
assert.equal(dynamicMap.buckets.length, 4); // <-- no rehash
233-
assert.equal(dynamicMap.getLoadFactor(), 3/4);
233+
assert.equal(dynamicMap.getLoadFactor(), 3 / 4);
234234
console.log(hashMap.collisions);
235235

236236
dynamicMap.set('cuatro', 4);
237237
assert.equal(dynamicMap.buckets.length, 8); // <-- rehash took place
238-
assert.equal(dynamicMap.getLoadFactor(), 4/8);
238+
assert.equal(dynamicMap.getLoadFactor(), 4 / 8);
239239
console.log(hashMap.collisions);
240240

241241
dynamicMap.set('cinco', 5);
242242
assert.equal(dynamicMap.buckets.length, 8); // <-- no rehash
243-
assert.equal(dynamicMap.getLoadFactor(), 5/8);
244-
console.log(hashMap.collisions);
243+
assert.equal(dynamicMap.getLoadFactor(), 5 / 8);
244+
console.log(hashMap.collisions);

‎src/data-structures/hash-maps/hash-map.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* @author Adrian Mejia <me AT adrianmejia.com>
44
*/
55
class HashMap {
6-
76
/**
87
* Initialize array that holds the values. Default is size 16
98
* @param {number} initialCapacity initial size of the array
@@ -50,23 +49,23 @@ class HashMap {
5049
* @param {any} value
5150
*/
5251
set(key, value) {
53-
const {bucketIndex, entryIndex} = this._getIndexes(key);
52+
const { bucketIndex, entryIndex } = this._getIndexes(key);
5453

55-
if(entryIndex === undefined) {
54+
if (entryIndex === undefined) {
5655
// initialize array and save key/value
57-
const keyIndex = this.keys.push({content: key}) - 1; // keep track of the key index
56+
const keyIndex = this.keys.push({ content: key }) - 1; // keep track of the key index
5857
this.buckets[bucketIndex] = this.buckets[bucketIndex] || [];
59-
this.buckets[bucketIndex].push({key, value, keyIndex});
58+
this.buckets[bucketIndex].push({ key, value, keyIndex });
6059
this.size++;
6160
// Optional: keep count of collisions
62-
if(this.buckets[bucketIndex].length > 1) { this.collisions++; }
61+
if (this.buckets[bucketIndex].length > 1) { this.collisions++; }
6362
} else {
6463
// override existing value
6564
this.buckets[bucketIndex][entryIndex].value = value;
6665
}
6766

6867
// check if a rehash is due
69-
if(this.loadFactor > 0 && this.getLoadFactor() > this.loadFactor) {
68+
if (this.loadFactor > 0 && this.getLoadFactor() > this.loadFactor) {
7069
this.rehash(this.buckets.length * 2);
7170
}
7271

@@ -79,9 +78,9 @@ class HashMap {
7978
* @param {any} key
8079
*/
8180
get(key) {
82-
const {bucketIndex, entryIndex} = this._getIndexes(key);
81+
const { bucketIndex, entryIndex } = this._getIndexes(key);
8382

84-
if(entryIndex === undefined) {
83+
if (entryIndex === undefined) {
8584
return;
8685
}
8786

@@ -107,22 +106,22 @@ class HashMap {
107106

108107
for (let entryIndex = 0; entryIndex < values.length; entryIndex++) {
109108
const entry = values[entryIndex];
110-
if(entry.key === key) {
111-
return {bucketIndex, entryIndex, keyIndex: entry.keyIndex};
109+
if (entry.key === key) {
110+
return { bucketIndex, entryIndex, keyIndex: entry.keyIndex };
112111
}
113112
}
114113

115-
return {bucketIndex};
114+
return { bucketIndex };
116115
}
117116

118117
/**
119118
* Returns true if an element in the Map object existed and has been removed, or false if the element does not exist.
120119
* @param {any} key
121120
*/
122121
delete(key) {
123-
const {bucketIndex, entryIndex, keyIndex} = this._getIndexes(key);
122+
const { bucketIndex, entryIndex, keyIndex } = this._getIndexes(key);
124123

125-
if(entryIndex === undefined) {
124+
if (entryIndex === undefined) {
126125
return false;
127126
}
128127

@@ -140,7 +139,7 @@ class HashMap {
140139
rehash(newCapacity) {
141140
const newMap = new HashMap(newCapacity);
142141

143-
this.keys.forEach(key => {
142+
this.keys.forEach((key) => {
144143
newMap.set(key.content, this.get(key.content));
145144
});
146145

‎src/data-structures/hash-maps/hash-map.spec.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ describe('without collisions', () => {
1818
expect(hashMap.getLoadFactor()).toBe(0);
1919
expect(hashMap.size).toBe(0);
2020
hashMap.set('test', 'one');
21-
expect(hashMap.getLoadFactor()).toBe(1/16);
21+
expect(hashMap.getLoadFactor()).toBe(1 / 16);
2222
expect(hashMap.size).toBe(1);
2323
});
2424

25-
it('should overwrite values and keep same size', () =>{
25+
it('should overwrite values and keep same size', () => {
2626
hashMap.set('test', 'uno');
2727
expect(hashMap.get('test')).toBe('uno');
2828
hashMap.set('test', 'dos');
2929
expect(hashMap.get('test')).toBe('dos');
3030
expect(hashMap.size).toBe(1);
3131
});
3232

33-
it('should return with has', () =>{
33+
it('should return with has', () => {
3434
expect(hashMap.has('test')).toBe(false);
3535
hashMap.set('test', 'uno');
3636
expect(hashMap.has('test')).toBe(true);
@@ -45,7 +45,7 @@ describe('without collisions', () => {
4545
expect(hashMap.delete('Bailando')).toBe(false);
4646
expect(hashMap.get('Bailando')).toBe(undefined);
4747

48-
expect(hashMap.keys.map(k => k.content)).toEqual(["Despacito", , "Dura"]);
48+
expect(hashMap.keys.map(k => k.content)).toEqual(['Despacito', , 'Dura']);
4949
});
5050
});
5151

@@ -76,19 +76,19 @@ describe('with many values (and collisions)', () => {
7676
expect(hashMap.getLoadFactor()).toBe(4);
7777
expect(hashMap.size).toBe(8);
7878
hashMap.set('test', 'one');
79-
expect(hashMap.getLoadFactor()).toBe(9/2);
79+
expect(hashMap.getLoadFactor()).toBe(9 / 2);
8080
expect(hashMap.size).toBe(9);
8181
});
8282

83-
it('should overwrite values and keep same size', () =>{
83+
it('should overwrite values and keep same size', () => {
8484
hashMap.set('test', 'uno');
8585
expect(hashMap.get('test')).toBe('uno');
8686
hashMap.set('test', 'dos');
8787
expect(hashMap.get('test')).toBe('dos');
8888
expect(hashMap.size).toBe(9);
8989
});
9090

91-
it('should return with has', () =>{
91+
it('should return with has', () => {
9292
expect(hashMap.has('test')).toBe(false);
9393
hashMap.set('test', 'uno');
9494
expect(hashMap.has('test')).toBe(true);
@@ -115,18 +115,18 @@ describe('#rehash', () => {
115115
});
116116

117117
it('should rehash after 12 items by default', () => {
118-
expect(hashMap.getLoadFactor()).toBe(11/16);
118+
expect(hashMap.getLoadFactor()).toBe(11 / 16);
119119
expect(hashMap.buckets.length).toBe(16);
120120
hashMap.set('Alone', 'Alan Walker');
121121
expect(hashMap.getLoadFactor()).toBe(0.75);
122122

123123
hashMap.set('Levels', 'Avicii');
124124

125-
expect(hashMap.getLoadFactor()).toBe(13/32);
125+
expect(hashMap.getLoadFactor()).toBe(13 / 32);
126126
expect(hashMap.buckets.length).toBe(32);
127127

128128
expect(hashMap.get('Dura')).toBe('Daddy Yankee');
129129
expect(hashMap.get('Bailando')).toBe('Enrique Iglesias');
130130
expect(hashMap.get('Levels')).toBe('Avicii');
131131
});
132-
});
132+
});

‎src/data-structures/linked-lists/linked-list-1.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class LinkedList {
1616
addLast(value) {
1717
const node = new Node(value);
1818

19-
if(this.first) {
19+
if (this.first) {
2020
let currentNode = this.first;
21-
while(currentNode && currentNode.next) {
21+
while (currentNode && currentNode.next) {
2222
currentNode = currentNode.next;
2323
}
2424
currentNode.next = node;
@@ -34,7 +34,7 @@ class LinkedList {
3434
removeFirst() {
3535
const first = this.first;
3636

37-
if(first) {
37+
if (first) {
3838
this.first = first.next;
3939
return first.value;
4040
}
@@ -60,8 +60,8 @@ class LinkedList {
6060
let current = this.first;
6161
let target;
6262

63-
if(current && current.next) {
64-
while(current && current.next && current.next.next) {
63+
if (current && current.next) {
64+
while (current && current.next && current.next.next) {
6565
current = current.next;
6666
}
6767
target = current.next;
@@ -71,7 +71,7 @@ class LinkedList {
7171
target = current;
7272
}
7373

74-
if(target) {
74+
if (target) {
7575
return target.value;
7676
}
7777
}
@@ -83,8 +83,8 @@ class LinkedList {
8383
* @param {any} value
8484
*/
8585
contains(value) {
86-
for (let current = this.first, index = 0; current; index++, current = current.next) {
87-
if(current.value === value) {
86+
for (let current = this.first, index = 0; current; index++, current = current.next) {
87+
if (current.value === value) {
8888
return index;
8989
}
9090
}
@@ -97,13 +97,13 @@ class LinkedList {
9797
* @param {any} nth
9898
*/
9999
removeAt(nth) {
100-
if(nth === 0) {
100+
if (nth === 0) {
101101
return this.removeFirst();
102102
}
103103

104-
for (let current = this.first, index = 0; current; index++, current = current.next) {
105-
if(index === nth) {
106-
if(!current.next) { // if it doesn't have next it means that it is the last
104+
for (let current = this.first, index = 0; current; index++, current = current.next) {
105+
if (index === nth) {
106+
if (!current.next) { // if it doesn't have next it means that it is the last
107107
return this.removeLast();
108108
}
109109
current.previous = current.next;
@@ -113,4 +113,4 @@ class LinkedList {
113113
}
114114
}
115115

116-
module.exports = LinkedList;
116+
module.exports = LinkedList;

‎src/data-structures/linked-lists/linked-list-2.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class LinkedList {
1818
addLast(value) {
1919
const node = new Node(value);
2020

21-
if(this.first) {
22-
let currentNode = this.first;
21+
if (this.first) {
22+
const currentNode = this.first;
2323
this.last.next = node;
2424
this.last = node;
2525
} else {
@@ -35,12 +35,11 @@ class LinkedList {
3535
removeFirst() {
3636
const first = this.first;
3737

38-
if(first) {
38+
if (first) {
3939
this.first = first.next;
4040
return first.value;
41-
} else {
42-
this.last = null;
4341
}
42+
this.last = null;
4443
}
4544

4645
/**
@@ -51,7 +50,7 @@ class LinkedList {
5150
addFirst(value) {
5251
const node = new Node(value);
5352
node.next = this.first;
54-
if(!this.first) {
53+
if (!this.first) {
5554
this.last = node;
5655
}
5756
this.first = node;
@@ -66,8 +65,8 @@ class LinkedList {
6665
let current = this.first;
6766
let target;
6867

69-
if(current && current.next) {
70-
while(current && current.next && current.next.next) {
68+
if (current && current.next) {
69+
while (current && current.next && current.next.next) {
7170
current = current.next;
7271
}
7372
this.last = current;
@@ -79,20 +78,20 @@ class LinkedList {
7978
target = current;
8079
}
8180

82-
if(target) {
81+
if (target) {
8382
return target.value;
8483
}
8584
}
8685

87-
/**
86+
/**
8887
* Find first occurence of the element matching the value
8988
* return index or undefined
9089
* Runtime: O(n)
9190
* @param {any} value
9291
*/
9392
contains(value) {
94-
for (let current = this.first, index = 0; current; index++, current = current.next) {
95-
if(current.value === value) {
93+
for (let current = this.first, index = 0; current; index++, current = current.next) {
94+
if (current.value === value) {
9695
return index;
9796
}
9897
}
@@ -105,13 +104,13 @@ class LinkedList {
105104
* @param {any} nth
106105
*/
107106
removeAt(nth) {
108-
if(nth === 0) {
107+
if (nth === 0) {
109108
return this.removeFirst();
110109
}
111110

112-
for (let current = this.first, index = 0; current; index++, current = current.next) {
113-
if(index === nth) {
114-
if(!current.next) { // if it doesn't have next it means that it is the last
111+
for (let current = this.first, index = 0; current; index++, current = current.next) {
112+
if (index === nth) {
113+
if (!current.next) { // if it doesn't have next it means that it is the last
115114
return this.removeLast();
116115
}
117116
current.previous = current.next;
@@ -128,4 +127,4 @@ LinkedList.prototype.remove = LinkedList.prototype.pop = LinkedList.prototype.re
128127
LinkedList.prototype.unshift = LinkedList.prototype.addFirst;
129128
LinkedList.prototype.shift = LinkedList.prototype.removeFirst;
130129

131-
module.exports = LinkedList;
130+
module.exports = LinkedList;

‎src/data-structures/linked-lists/linked-list.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class LinkedList {
1919
addLast(value) {
2020
const newNode = new Node(value);
2121

22-
if(this.first) {
22+
if (this.first) {
2323
newNode.previous = this.last;
2424
this.last.next = newNode;
2525
this.last = newNode;
@@ -40,18 +40,17 @@ class LinkedList {
4040
removeFirst() {
4141
const first = this.first;
4242

43-
if(first) {
43+
if (first) {
4444
this.first = first.next;
45-
if(this.first) {
45+
if (this.first) {
4646
this.first.previous = null;
4747
}
4848

4949
this.size--;
5050

5151
return first.value;
52-
} else {
53-
this.last = null;
5452
}
53+
this.last = null;
5554
}
5655

5756
/**
@@ -64,7 +63,7 @@ class LinkedList {
6463

6564
node.next = this.first;
6665

67-
if(this.first) {
66+
if (this.first) {
6867
this.first.previous = node;
6968
} else {
7069
this.last = node;
@@ -86,7 +85,7 @@ class LinkedList {
8685
let current = this.first;
8786
let target;
8887

89-
if(current && current.next) {
88+
if (current && current.next) {
9089
current = this.last.previous;
9190
this.last = current;
9291
target = current.next;
@@ -97,7 +96,7 @@ class LinkedList {
9796
target = current;
9897
}
9998

100-
if(target) {
99+
if (target) {
101100
this.size--;
102101
return target.value;
103102
}
@@ -110,8 +109,8 @@ class LinkedList {
110109
* @param {any} value
111110
*/
112111
contains(value) {
113-
for (let current = this.first, index = 0; current; index++, current = current.next) {
114-
if(current.value === value) {
112+
for (let current = this.first, index = 0; current; index++, current = current.next) {
113+
if (current.value === value) {
115114
return index;
116115
}
117116
}
@@ -124,13 +123,13 @@ class LinkedList {
124123
* @param {any} index
125124
*/
126125
remove(index = 0) {
127-
if(index === 0) {
126+
if (index === 0) {
128127
return this.removeFirst();
129128
}
130129

131-
for (let current = this.first, i = 0; current; i++, current = current.next) {
132-
if(i === index) {
133-
if(!current.next) { // if it doesn't have next it means that it is the last
130+
for (let current = this.first, i = 0; current; i++, current = current.next) {
131+
if (i === index) {
132+
if (!current.next) { // if it doesn't have next it means that it is the last
134133
return this.removeLast();
135134
}
136135
current.previous = current.next;
@@ -147,27 +146,26 @@ class LinkedList {
147146
* @param {Number} index position to insert element
148147
*/
149148
add(value, index = 0) {
150-
if(index === 0) {
149+
if (index === 0) {
151150
return this.addFirst(value);
152151
}
153152

154-
for (let current = this.first, i = 0; i <= this.size; i++, current = (current && current.next)) {
155-
if(i === index) {
156-
if(i === this.size) { // if it doesn't have next it means that it is the last
153+
for (let current = this.first, i = 0; i <= this.size; i++, current = (current && current.next)) {
154+
if (i === index) {
155+
if (i === this.size) { // if it doesn't have next it means that it is the last
157156
return this.addLast(value);
158157
}
159158
const newNode = new Node(value);
160159
newNode.previous = current.previous;
161160
newNode.next = current;
162161

163162
current.previous.next = newNode;
164-
if(current.next) { current.next.previous = newNode; }
163+
if (current.next) { current.next.previous = newNode; }
165164
this.size++;
166165
return newNode;
167166
}
168167
}
169168
}
170-
171169
}
172170

173171
// Aliases
@@ -177,4 +175,4 @@ LinkedList.prototype.unshift = LinkedList.prototype.addFirst;
177175
LinkedList.prototype.shift = LinkedList.prototype.removeFirst;
178176
LinkedList.prototype.search = LinkedList.prototype.contains;
179177

180-
module.exports = LinkedList;
178+
module.exports = LinkedList;

‎src/data-structures/linked-lists/linked-list.spec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const LinkedList = require('./linked-list');
22

3-
describe('LinkedList', function () {
3+
describe('LinkedList', () => {
44
let linkedList;
55

66
beforeEach(() => {
@@ -169,7 +169,6 @@ describe('LinkedList', function () {
169169
});
170170

171171
describe('Doubly Linked List and aliases', () => {
172-
173172
describe('#addLast', () => {
174173
beforeEach(() => {
175174
linkedList.addLast('a');
@@ -267,4 +266,4 @@ describe('LinkedList', function () {
267266
});
268267
});
269268
});
270-
});
269+
});

‎src/data-structures/linked-lists/node-1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ class Node {
88
}
99
}
1010

11-
module.exports = Node;
11+
module.exports = Node;

‎src/data-structures/linked-lists/node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ class Node {
99
}
1010
}
1111

12-
module.exports = Node;
12+
module.exports = Node;

‎src/data-structures/queues/queue-1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ class Queue {
3232
}
3333
}
3434

35-
module.exports = Queue;
35+
module.exports = Queue;

‎src/data-structures/queues/queue-2.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class Queue {
2222
* Amortized runtime: O(1)*
2323
*/
2424
remove() {
25-
if(!this.output.length) {
26-
while(this.input.length) {
25+
if (!this.output.length) {
26+
while (this.input.length) {
2727
this.output.push(this.input.pop());
2828
}
2929
}
@@ -42,4 +42,4 @@ class Queue {
4242
Queue.prototype.enqueue = Queue.prototype.add;
4343
Queue.prototype.dequeue = Queue.prototype.remove;
4444

45-
module.exports = Queue;
45+
module.exports = Queue;

‎src/data-structures/queues/queue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ class Queue {
4444
Queue.prototype.enqueue = Queue.prototype.add;
4545
Queue.prototype.dequeue = Queue.prototype.remove;
4646

47-
module.exports = Queue;
47+
module.exports = Queue;

‎src/data-structures/queues/queue.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Queue = require('./queue.js');
22

3-
describe('Queue', function () {
3+
describe('Queue', () => {
44
let queue;
55

66
beforeEach(() => {
@@ -32,7 +32,7 @@ describe('Queue', function () {
3232
expect(queue.remove()).toEqual('b');
3333
expect(queue.remove()).toEqual('c');
3434
expect(queue.remove()).toBe(undefined);
35-
})
35+
});
3636
});
3737

3838
describe('#isEmpty', () => {
@@ -46,4 +46,4 @@ describe('Queue', function () {
4646
expect(queue.isEmpty()).toBe(false);
4747
});
4848
});
49-
});
49+
});

‎src/data-structures/sets/set.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const MySet = require('./set');
22

3-
describe('Set', function () {
3+
describe('Set', () => {
44
let set;
55

66
beforeEach(() => {
@@ -40,7 +40,7 @@ describe('Set', function () {
4040
set.add(1);
4141
set.add(2);
4242
set.add(3);
43-
expect(set.entries()).toEqual([1,2,3]);
43+
expect(set.entries()).toEqual([1, 2, 3]);
4444
});
4545

4646
it('should return entries wihout holes', () => {
@@ -52,7 +52,7 @@ describe('Set', function () {
5252
expect(set.delete(2)).toBe(true);
5353
expect(set.delete(0)).toBe(true);
5454

55-
expect(set.entries()).toEqual([1,3]);
55+
expect(set.entries()).toEqual([1, 3]);
5656
expect(set.size).toBe(2);
5757
});
58-
});
58+
});

‎src/data-structures/stacks/stack.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Stack = require('./stack.js');
22

3-
describe('Stack', function () {
3+
describe('Stack', () => {
44
let stack;
55

66
beforeEach(() => {
@@ -40,4 +40,4 @@ describe('Stack', function () {
4040
expect(stack.isEmpty()).toBe(false);
4141
});
4242
});
43-
});
43+
});

0 commit comments

Comments
 (0)
Please sign in to comment.