Skip to content

Commit fe995f0

Browse files
authoredApr 4, 2019
Merge pull request amejiarosario#2 from amejiarosario/eslint-fixes
Eslint fixes

38 files changed

+312
-240
lines changed
 

‎.eslintignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/runtimes/**
2+
src/data-structures/maps/hash-maps/hashing.js
3+
src/data-structures/maps/hash-maps/*-test.js
4+
src/data-structures/maps/hash-maps/hash-map-*.js
5+
src/data-structures/linked-lists/linked-list-*.js
6+
src/data-structures/custom/lru-cache-*.js

‎.eslintrc.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
module.exports = {
2-
"extends": "airbnb-base",
3-
"env": {
4-
"jest": true
2+
extends: 'airbnb-base',
3+
env: {
4+
jest: true
5+
},
6+
globals: {
7+
BigInt: true,
58
},
69
rules: {
710
// https://github.com/airbnb/javascript/issues/1089
811

912
// https://stackoverflow.com/a/35637900/684957
1013
// allow to add properties to arguments
11-
"no-param-reassign": [2, { "props": false }],
14+
'no-param-reassign': [2, { 'props': false }],
1215

1316
// https://eslint.org/docs/rules/no-plusplus
1417
// allows unary operators ++ and -- in the afterthought (final expression) of a for loop.
15-
"no-plusplus": [2, { "allowForLoopAfterthoughts": true }],
18+
'no-plusplus': [2, { 'allowForLoopAfterthoughts': true }],
1619

1720
// Allow for..of
18-
"no-restricted-syntax": [0, "ForOfStatement"],
19-
},
20-
globals: {
21-
BigInt: true,
21+
'no-restricted-syntax': [0, 'ForOfStatement'],
2222
}
2323
};

‎.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: node_js
2+
script: npm run ci
23
node_js:
34
- "node"
45
- "lts/*"

‎.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"workbench.colorCustomizations": {
3+
"titleBar.activeBackground": "#f9e64f",
4+
"titleBar.inactiveBackground": "#f9e64f99",
5+
"titleBar.activeForeground": "#15202b",
6+
"titleBar.inactiveForeground": "#15202b99"
7+
}
8+
}

‎src/changelog.md renamed to ‎CHANGELOG.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,51 @@
11
# Changelog
2+
23
All notable changes to this project will be documented in this file.
34

45
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
56
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
67

78
## [Unreleased]
8-
### Changed
9-
- Fix picture on https://www.npmjs.com/package/dsa.js
10-
### Added
11-
- CI: travis
12-
### Removed
13-
-
9+
10+
### Breaking changes
11+
12+
### New Features
13+
- Added Eslint and Travis ci
14+
15+
### Patches
16+
- Readme improvements
17+
- Badges for npm versions and build status
1418

1519
## [1.1.0] - 2019-03-29
20+
1621
### Added
22+
1723
- README.md added because NPM packages doesn't read README.adoc
1824
- Public API to use the package `dsa.js`
1925
- This changelog
2026

2127
### Changed
28+
2229
- Updated dependencies (removed lodash since is not needed)
2330

2431
### Removed
32+
2533
-
2634

2735
## [1.0.0] - 2019-03-29
36+
2837
### Added
38+
2939
- Started the project
3040
- Book released
3141
- npm package published
3242

3343
### Changed
44+
3445
-
46+
3547
### Removed
48+
3649
-
3750

3851

‎README.md

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
# Data Structures and Algorithms in JavaScript
22

3+
[![Build Status](https://travis-ci.com/amejiarosario/dsa.js.svg?branch=master)](https://travis-ci.com/amejiarosario/dsa.js)
4+
[![npm version](https://badge.fury.io/js/dsa.js.svg)](https://badge.fury.io/js/dsa.js)
5+
36
This repository covers the implementation of the classical algorithms and data structures in JavaScript.
47

5-
<!-- This goes along with [these posts series](https://adrianmejia.com/tags/tutorial-algorithms/) that explain each implementation in details and also this [book](https://gum.co/dsajs) that explain these topics and some with more examples and illustrations. -->
8+
## Usage
9+
10+
You can clone the repo or install the code from NPM:
11+
12+
```sh
13+
npm install dsa.js
14+
```
15+
16+
and then you can import it into your programs or CLI
17+
18+
```js
19+
const { LinkedList, Queue, Stack } = require('dsa.js');
20+
```
21+
22+
For a full list of all the exposed data structures and algorithms [see](https://github.com/amejiarosario/dsa.js/blob/master/src/index.js).
623

724
## Book
825

9-
You can check out the book that goes deeper into each topic and provide addtional illustrations and explanations.
26+
You can check out the book that goes deeper into each topic and provide additional illustrations and explanations.
1027

1128
- Algorithmic toolbox to avoid getting stuck while coding.
1229
- Explains data structures similarities and differences.
@@ -21,65 +38,67 @@ We are covering the following data structures.
2138
[![Interactive Data Structures](https://user-images.githubusercontent.com/418605/46118890-ba721180-c1d6-11e8-82bc-6a671428b422.png)](https://embed.kumu.io/85f1a4de5fb8430a10a1bf9c5118e015)
2239

2340
### Linear Data Structures
24-
1. **Arrays**: Built-in in most languages so not implemented here.
41+
42+
1. **Arrays**: Built-in in most languages so not implemented here.
2543
[Post](https://adrianmejia.com/blog/2018/04/28/data-structures-time-complexity-for-beginners-arrays-hashmaps-linked-lists-stacks-queues-tutorial/#Array).
2644

27-
2. **Linked Lists**: each data node has a link to the next (and
45+
2. **Linked Lists**: each data node has a link to the next (and
2846
previous).
2947
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/data-structures/linked-lists/linked-list.js)
3048
|
3149
[Post](https://adrianmejia.com/blog/2018/04/28/data-structures-time-complexity-for-beginners-arrays-hashmaps-linked-lists-stacks-queues-tutorial/#Linked-Lists).
3250

33-
3. **Queue**: data flows in a "first-in, first-out" (FIFO) manner.
51+
3. **Queue**: data flows in a "first-in, first-out" (FIFO) manner.
3452
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/data-structures/queues/queue.js)
3553
|
3654
[Post](https://adrianmejia.com/blog/2018/04/28/data-structures-time-complexity-for-beginners-arrays-hashmaps-linked-lists-stacks-queues-tutorial/#Queues)
3755

38-
4. **Stacks**: data flows in a "last-in, first-out" (LIFO) manner.
56+
4. **Stacks**: data flows in a "last-in, first-out" (LIFO) manner.
3957
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/data-structures/stacks/stack.js)
4058
|
4159
[Post](https://adrianmejia.com/blog/2018/04/28/data-structures-time-complexity-for-beginners-arrays-hashmaps-linked-lists-stacks-queues-tutorial/#Stacks).
4260

4361
### Non-Linear Data Structures
44-
1. **Trees**: data nodes has zero or more adjacent nodes a.k.a.
62+
63+
1. **Trees**: data nodes has zero or more adjacent nodes a.k.a.
4564
children. Each node can only have one parent node otherwise is a
4665
graph not a tree.
4766
[Code](https://github.com/amejiarosario/algorithms.js/tree/master/src/data-structures/trees)
4867
|
4968
[Post](https://adrianmejia.com/blog/2018/06/11/data-structures-for-beginners-trees-binary-search-tree-tutorial/)
5069

51-
1. **Binary Trees**: same as tree but only can have two children at
70+
1. **Binary Trees**: same as tree but only can have two children at
5271
most.
5372
[Code](https://github.com/amejiarosario/algorithms.js/tree/master/src/data-structures/trees)
5473
|
5574
[Post](https://adrianmejia.com/blog/2018/06/11/data-structures-for-beginners-trees-binary-search-tree-tutorial/#Binary-Trees)
5675

57-
2. **Binary Search Trees** (BST): same as binary tree, but the
76+
2. **Binary Search Trees** (BST): same as binary tree, but the
5877
nodes value keep this order `left < parent < rigth`.
5978
[Code](https://github.com/amejiarosario/algorithms.js/blob/master/src/data-structures/trees/binary-search-tree.js)
6079
|
6180
[Post](https://adrianmejia.com/blog/2018/06/11/data-structures-for-beginners-trees-binary-search-tree-tutorial/#Binary-Search-Tree-BST)
6281

63-
3. **AVL Trees**: Self-balanced BST to maximize look up time.
82+
3. **AVL Trees**: Self-balanced BST to maximize look up time.
6483
[Code](https://github.com/amejiarosario/algorithms.js/blob/master/src/data-structures/trees/avl-tree.js)
6584
|
6685
[Post](https://adrianmejia.com/blog/2018/07/16/self-balanced-binary-search-trees-with-avl-tree-data-structure-for-beginners/)
6786

68-
4. **Red-Black Trees**: Self-balanced BST more loose than AVL to
87+
4. **Red-Black Trees**: Self-balanced BST more loose than AVL to
6988
maximize insertion speed.
7089
[Code](https://github.com/amejiarosario/algorithms.js/blob/master/src/data-structures/trees/red-black-tree.js)
7190

72-
2. **Maps**: key-value store.
91+
2. **Maps**: key-value store.
7392

74-
1. **Hash Maps**: implements map using a hash function.
93+
1. **Hash Maps**: implements map using a hash function.
7594
[Code](https://github.com/amejiarosario/algorithms.js/blob/master/src/data-structures/hash-maps/hashmap.js)
7695
|
7796
[Post](https://adrianmejia.com/blog/2018/04/28/data-structures-time-complexity-for-beginners-arrays-hashmaps-linked-lists-stacks-queues-tutorial/#HashMaps)
7897

79-
2. **Tree Maps**: implement map using a self-balanced BST.
98+
2. **Tree Maps**: implement map using a self-balanced BST.
8099
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/data-structures/maps/tree-maps/tree-map.js)
81100

82-
3. **Graphs**: data **nodes** that can have a connection or **edge** to
101+
3. **Graphs**: data **nodes** that can have a connection or **edge** to
83102
zero or more adjacent nodes. Unlike trees, nodes can have multiple
84103
parents, loops.
85104
[Code](https://github.com/amejiarosario/algorithms.js/blob/master/src/data-structures/graphs/graph.js)
@@ -88,39 +107,39 @@ We are covering the following data structures.
88107

89108
## Algorithms
90109

91-
- Sorting algorithms
110+
- Sorting algorithms
92111

93-
- Bubble Sort.
94-
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/sorting/bubble-sort.js)
112+
- Bubble Sort.
113+
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/sorting/bubble-sort.js)
95114

96-
- Insertion Sort.
97-
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/sorting/insertion-sort.js)
115+
- Insertion Sort.
116+
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/sorting/insertion-sort.js)
98117

99-
- Selection Sort.
100-
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/sorting/selection-sort.js)
118+
- Selection Sort.
119+
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/sorting/selection-sort.js)
101120

102-
- Merge Sort.
103-
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/sorting/merge-sort.js)
121+
- Merge Sort.
122+
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/sorting/merge-sort.js)
104123

105-
- Quicksort.
106-
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/sorting/quick-sort.js)
124+
- Quick sort.
125+
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/sorting/quick-sort.js)
107126

108-
- Greedy Algorithms
127+
- Greedy Algorithms
109128

110-
- Fractional Knapsack Problem.
111-
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/knapsack-fractional.js)
129+
- Fractional Knapsack Problem.
130+
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/knapsack-fractional.js)
112131

113-
- Divide and Conquer
132+
- Divide and Conquer
114133

115-
- Fibonacci Numbers.
116-
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/fibonacci-recursive.js)
134+
- Fibonacci Numbers.
135+
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/fibonacci-recursive.js)
117136

118-
- Dynamic Programming
137+
- Dynamic Programming
119138

120-
- Fibonacci with memoization.
121-
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/fibanacci-dynamic-programming.js)
139+
- Fibonacci with memoization.
140+
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/fibanacci-dynamic-programming.js)
122141

123-
- Backtracking algorithms
142+
- Backtracking algorithms
124143

125-
- Word permutations.
126-
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/permutations-backtracking.js)
144+
- Word permutations.
145+
[Code](https://github.com/amejiarosario/dsa.js/blob/master/src/algorithms/permutations-backtracking.js)

‎README.adoc renamed to ‎deprecated-README.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
:toclevels: 2
44
Adrian Mejia <https://github.com/amejiarosario[@amejiarosario]>
55

6+
image:https://travis-ci.com/amejiarosario/dsa.js.svg?branch=master["Build Status", link="https://travis-ci.com/amejiarosario/dsa.js"]
7+
image:https://badge.fury.io/js/dsa.js.svg["npm version", link="https://badge.fury.io/js/dsa.js"]
8+
69
This repository covers the implementation of the classical algorithms and data structures in JavaScript.
710

811
toc::[]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎package-lock.json

Lines changed: 104 additions & 88 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,19 @@
22
"name": "dsa.js",
33
"version": "1.1.0",
44
"description": "Data Structures & Algorithms in JS",
5-
"main": "./src/index.js",
6-
"dependencies": {
7-
8-
},
9-
"devDependencies": {
10-
"benchmark": "2.1.4",
11-
"eslint": "4.19.1",
12-
"eslint-config-airbnb-base": "12.1.0",
13-
"eslint-plugin-import": "2.12.0",
14-
"eslint-plugin-jest": "21.17.0",
15-
"jest": "23.6.0",
16-
"textlint-plugin-asciidoctor": "1.0.2"
5+
"author": "Adrian Mejia <hi+dsajs@adrianmejia.com> (https://adrianmejia.com)",
6+
"homepage": "https://github.com/amejiarosario/dsa.js",
7+
"repository": {
8+
"type" : "git",
9+
"url" : "https://github.com/amejiarosario/dsa.js.git"
1710
},
11+
"main": "./src/index.js",
1812
"scripts": {
19-
"test": "jest src/ # jest # mocha src/**/*spec.js # jasmine JASMINE_CONFIG_PATH=jasmine.json # node jasmine-runner.js",
13+
"test": "jest src/",
2014
"watch": "jest src/ --watch --coverage",
2115
"coverage": "jest src/ --coverage && open coverage/lcov-report/index.html",
22-
"linter": "npx eslint --fix -f codeframe src/"
16+
"lint": "npx eslint --fix --format codeframe src/",
17+
"ci": "npx eslint src/ && npm test"
2318
},
2419
"keywords": [
2520
"algorithms",
@@ -29,6 +24,20 @@
2924
"linked lists",
3025
"binary search trees"
3126
],
32-
"author": "Adrian Mejia <hi+dsajs@adrianmejia.com>",
33-
"license": "ISC"
27+
"license": "MIT",
28+
"dependencies": {
29+
30+
},
31+
"devDependencies": {
32+
"benchmark": "2.1.4",
33+
"eslint": "4.19.1",
34+
"eslint-config-airbnb-base": "^13.1.0",
35+
"eslint-plugin-import": "^2.16.0",
36+
"eslint-plugin-jest": "21.17.0",
37+
"jest": "23.6.0",
38+
"textlint-plugin-asciidoctor": "1.0.2"
39+
},
40+
"engines": {
41+
"node" : ">=10.0.0"
42+
}
3443
}

‎src/algorithms/sorting/merge-sort.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ function merge(array1, array2 = []) {
1616
// merge elements on a and b in asc order. Run-time O(a + b)
1717
for (let index = 0, i1 = 0, i2 = 0;
1818
index < mergedLength; index++) { // <1>
19-
if (i2 >= array2.length ||
20-
(i1 < array1.length && array1[i1] <= array2[i2])) {
19+
if (i2 >= array2.length
20+
|| (i1 < array1.length && array1[i1] <= array2[i2])) {
2121
mergedArray[index] = array1[i1]; // <2>
2222
i1 += 1;
2323
} else {
@@ -45,7 +45,7 @@ function splitSort(array) {
4545
// base case
4646
if (size < 2) {
4747
return array;
48-
} else if (size === 2) {
48+
} if (size === 2) {
4949
return array[0] < array[1] ? array : [array[1], array[0]]; // <1>
5050
}
5151

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
21
/**
3-
* Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
2+
* Design and implement a data structure for Least Recently Used (LRU) cache.
3+
* It should support the following operations: get and put.
44
5-
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
6-
put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
5+
get(key) - Get the value (will always be positive) of the key
6+
if the key exists in the cache, otherwise return -1.
7+
put(key, value) - Set or insert the value if the key is not already present.
8+
When the cache reached its capacity, it should invalidate the least
9+
recently used item before inserting a new item.
710
811
Follow up:
912
Could you do both operations in O(1) time complexity?
@@ -27,46 +30,41 @@
2730
*
2831
* @param {number} capacity
2932
*/
30-
const LRUCache = function (capacity) {
31-
this.map = new Map();
32-
this.capacity = capacity;
33-
};
33+
class LRUCache {
34+
constructor(capacity) {
35+
this.map = new Map();
36+
this.capacity = capacity;
37+
}
3438

35-
/**
36-
* @param {number} key
37-
* @return {number}
38-
*/
39-
LRUCache.prototype.get = function (key) {
40-
const value = this.map.get(key);
41-
if (value) {
42-
this.moveToTop(key);
43-
return value;
39+
get(key) {
40+
const value = this.map.get(key);
41+
if (value) {
42+
this.moveToTop(key);
43+
return value;
44+
}
45+
return -1;
4446
}
45-
return -1;
46-
};
4747

48-
/**
49-
* @param {number} key
50-
* @param {number} value
51-
* @return {void}
52-
*/
53-
LRUCache.prototype.put = function (key, value) {
54-
this.map.set(key, value);
55-
this.rotate(key);
56-
};
48+
put(key, value) {
49+
this.map.set(key, value);
50+
this.rotate(key);
51+
}
5752

58-
LRUCache.prototype.rotate = function (key) {
59-
this.moveToTop(key);
60-
while (this.map.size > this.capacity) {
61-
const it = this.map.keys();
62-
this.map.delete(it.next().value);
53+
rotate(key) {
54+
this.moveToTop(key);
55+
while (this.map.size > this.capacity) {
56+
const it = this.map.keys();
57+
this.map.delete(it.next().value);
58+
}
6359
}
64-
};
6560

66-
LRUCache.prototype.moveToTop = function (key) {
67-
if (this.map.has(key)) {
68-
const value = this.map.get(key);
69-
this.map.delete(key);
70-
this.map.set(key, value);
61+
moveToTop(key) {
62+
if (this.map.has(key)) {
63+
const value = this.map.get(key);
64+
this.map.delete(key);
65+
this.map.set(key, value);
66+
}
7167
}
72-
};
68+
}
69+
70+
module.exports = LRUCache;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const Node = require('./node');
21
const util = require('util');
2+
const Node = require('./node');
33

44
// tag::constructor[]
55
/**

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,3 @@ module.exports = HashMap;
7474
// assert.equal(hashMap.get('cat'), 8); // got overwritten by art 😱
7575
// assert.equal(hashMap.get('rat'), 8); // got overwritten by art 😱
7676
// assert.equal(hashMap.get('dog'), 8); // got overwritten by art 😱
77-

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable */
2+
13
// tag::naiveHashCode[]
24
/**
35
* Naïve implementation of a non-cryptographic hashing function

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Queue {
3232
dequeue() {
3333
return this.items.removeFirst();
3434
}
35+
3536
// end::dequeue[]
3637
/**
3738
* Size of the queue
@@ -66,4 +67,3 @@ queue.dequeue(); //↪️ b
6667
queue.dequeue(); //↪️ c
6768
// end::snippet[]
6869
// */
69-

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,3 @@ class ArraySet {
8585
}
8686

8787
module.exports = ArraySet;
88-

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,3 @@ class HashMapSet {
8787
}
8888

8989
module.exports = HashMapSet;
90-

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,3 @@ class MapSet {
7272
}
7373

7474
module.exports = MapSet;
75-

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,3 @@ Stack.prototype.push = Stack.prototype.add;
3737
Stack.prototype.pop = Stack.prototype.remove;
3838

3939
module.exports = Stack;
40-

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,3 @@ stack.remove(); //↪️ c
6767
stack.remove(); //↪️ a
6868
// end::snippet[]
6969
// */
70-

‎src/data-structures/trees/avl-tree.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ function balance(node) {
2424
// left subtree is higher than right subtree
2525
if (node.left.balanceFactor > 0) {
2626
return rightRotation(node);
27-
} else if (node.left.balanceFactor < 0) {
27+
} if (node.left.balanceFactor < 0) {
2828
return leftRightRotation(node);
2929
}
3030
} else if (node.balanceFactor < -1) {
3131
// right subtree is higher than left subtree
3232
if (node.right.balanceFactor < 0) {
3333
return leftRotation(node);
34-
} else if (node.right.balanceFactor > 0) {
34+
} if (node.right.balanceFactor > 0) {
3535
return rightLeftRotation(node);
3636
}
3737
}

‎src/data-structures/trees/binary-search-tree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class BinarySearchTree {
6969
findNodeAndParent(value, node = this.root, parent = null) {
7070
if (!node || node.value === value) {
7171
return { found: node, parent };
72-
} else if (value < node.value) {
72+
} if (value < node.value) {
7373
return this.findNodeAndParent(value, node.left, node);
7474
}
7575
return this.findNodeAndParent(value, node.right, node);

‎src/data-structures/trees/red-black-tree.spec.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,9 @@ describe('RedBlackTree', () => {
2727
});
2828

2929
it('should balance tree by rotating left', () => {
30-
const n1 = tree.add(1);
31-
const n2 = tree.add(2);
32-
const n3 = tree.add(3);
33-
34-
// console.log(n3)
35-
30+
tree.add(1);
31+
tree.add(2);
32+
tree.add(3);
3633
expect(tree.size).toBe(3);
3734

3835
expect(tree.toArray()).toEqual([

‎src/runtimes/01-is-empty.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Return true if an array is empty and false otherwise
3+
* Check for objects keys as well
34
* @param {array|string|object} thing
45
* @example
56
* isEmpty() // => true
@@ -30,4 +31,7 @@ function isEmpty(thing) {
3031
}
3132
// end::isEmpty[]
3233

33-
module.exports = isEmpty;
34+
module.exports = {
35+
isEmpty,
36+
isEmpty2,
37+
};

‎src/runtimes/02-binary-search.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ function binarySearchRecursive(array, search, offset = 0) {
1818

1919
if (current === search) {
2020
return offset + half;
21-
} else if (array.length < 2) {
21+
} if (array.length < 2) {
2222
return -1;
23-
} else if (search > current) {
23+
} if (search > current) {
2424
const right = array.slice(half);
2525
return binarySearchRecursive(right, search, offset + half);
2626
}
@@ -49,7 +49,7 @@ function binarySearchIterative(array, search) {
4949

5050
if (current === search) {
5151
return currentIndex;
52-
} else if (search > current) {
52+
} if (search > current) {
5353
start = currentIndex;
5454
} else if (search < current) {
5555
end = currentIndex;
@@ -62,24 +62,26 @@ function binarySearchIterative(array, search) {
6262
// const binarySearch = binarySearchRecursive;
6363
const binarySearch = binarySearchIterative;
6464

65-
function test() {
66-
const directory = ['Adrian', 'Bella', 'Charlotte', 'Daniel', 'Emma', 'Hanna', 'Isabella', 'Jayden', 'Kaylee', 'Luke', 'Mia', 'Nora', 'Olivia', 'Paisley', 'Riley', 'Thomas', 'Wyatt', 'Xander', 'Zoe'];
65+
// function test() {
66+
// const directory = ['Adrian', 'Bella', 'Charlotte', 'Daniel',
67+
// 'Emma', 'Hanna', 'Isabella', 'Jayden', 'Kaylee', 'Luke', 'Mia',
68+
// 'Nora', 'Olivia', 'Paisley', 'Riley', 'Thomas', 'Wyatt', 'Xander', 'Zoe'];
69+
//
70+
// const assert = require('assert');
71+
// assert.equal(binarySearch([], 'not found'), -1);
72+
// assert.equal(binarySearch([1], 2), -1);
73+
// assert.equal(binarySearch([1], 1), 0);
74+
// assert.equal(binarySearch([1, 2, 3], 1), 0);
75+
// assert.equal(binarySearch([1, 2, 3], 2), 1);
76+
// assert.equal(binarySearch([1, 2, 3], 3), 2);
77+
// assert.equal(binarySearch([1, 2, 3], 31), -1);
78+
// assert.equal(binarySearch(directory, 'Adrian'), 0);
79+
// assert.equal(binarySearch(directory, 'Hanna'), 5);
80+
// assert.equal(binarySearch(directory, 'Zoe'), 18);
81+
// assert.equal(binarySearch(directory, 'not found'), -1);
82+
// }
6783

68-
const assert = require('assert');
69-
assert.equal(binarySearch([], 'not found'), -1);
70-
assert.equal(binarySearch([1], 2), -1);
71-
assert.equal(binarySearch([1], 1), 0);
72-
assert.equal(binarySearch([1, 2, 3], 1), 0);
73-
assert.equal(binarySearch([1, 2, 3], 2), 1);
74-
assert.equal(binarySearch([1, 2, 3], 3), 2);
75-
assert.equal(binarySearch([1, 2, 3], 31), -1);
76-
assert.equal(binarySearch(directory, 'Adrian'), 0);
77-
assert.equal(binarySearch(directory, 'Hanna'), 5);
78-
assert.equal(binarySearch(directory, 'Zoe'), 18);
79-
assert.equal(binarySearch(directory, 'not found'), -1);
80-
}
81-
82-
test();
84+
// test();
8385

8486

85-
module.exports = binarySearch;
87+
module.exports = { binarySearch, binarySearchIterative, binarySearchRecursive };

‎src/runtimes/06-multi-variable-equation-solver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function findXYZ({ start = 0, end = 10 } = {}) {
1919
for (let x = start; x < end; x++) {
2020
for (let y = start; y < end; y++) {
2121
for (let z = start; z < end; z++) {
22-
if (3 * x + 9 * y + 8 * z === 79) {
22+
if (3 * x + 9 * y + 8 * z === 79) { // eslint-disable-line
2323
solutions.push({ x, y, z });
2424
}
2525
}

0 commit comments

Comments
 (0)
Please sign in to comment.