diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..38bb7054 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,6 @@ +/runtimes/** +src/data-structures/maps/hash-maps/hashing.js +src/data-structures/maps/hash-maps/*-test.js +src/data-structures/maps/hash-maps/hash-map-*.js +src/data-structures/linked-lists/linked-list-*.js +src/data-structures/custom/lru-cache-*.js diff --git a/.eslintrc.js b/.eslintrc.js index 0c7f7af4..bfc96a26 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,23 +1,23 @@ module.exports = { - "extends": "airbnb-base", - "env": { - "jest": true + extends: 'airbnb-base', + env: { + jest: true + }, + globals: { + BigInt: true, }, rules: { // https://github.com/airbnb/javascript/issues/1089 // https://stackoverflow.com/a/35637900/684957 // allow to add properties to arguments - "no-param-reassign": [2, { "props": false }], + 'no-param-reassign': [2, { 'props': false }], // https://eslint.org/docs/rules/no-plusplus // allows unary operators ++ and -- in the afterthought (final expression) of a for loop. - "no-plusplus": [2, { "allowForLoopAfterthoughts": true }], + 'no-plusplus': [2, { 'allowForLoopAfterthoughts': true }], // Allow for..of - "no-restricted-syntax": [0, "ForOfStatement"], - }, - globals: { - BigInt: true, + 'no-restricted-syntax': [0, 'ForOfStatement'], } }; diff --git a/package-lock.json b/package-lock.json index 75a7c5fd..32c1ad24 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "dsa.js", - "version": "0.0.1", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1555,12 +1555,14 @@ } }, "eslint-config-airbnb-base": { - "version": "12.1.0", - "resolved": "https://registry.npmjs.org/eslint-config-airbnb-base/-/eslint-config-airbnb-base-12.1.0.tgz", - "integrity": "sha512-/vjm0Px5ZCpmJqnjIzcFb9TKZrKWz0gnuG/7Gfkt0Db1ELJR51xkZth+t14rYdqWgX836XbuxtArbIHlVhbLBA==", + "version": "13.1.0", + "resolved": "https://registry.npmjs.org/eslint-config-airbnb-base/-/eslint-config-airbnb-base-13.1.0.tgz", + "integrity": "sha512-XWwQtf3U3zIoKO1BbHh6aUhJZQweOwSt4c2JrPDg9FP3Ltv3+YfEv7jIDB8275tVnO/qOHbfuYg3kzw6Je7uWw==", "dev": true, "requires": { - "eslint-restricted-globals": "^0.1.1" + "eslint-restricted-globals": "^0.1.1", + "object.assign": "^4.1.0", + "object.entries": "^1.0.4" } }, "eslint-import-resolver-node": { @@ -1573,73 +1575,49 @@ "resolve": "^1.5.0" }, "dependencies": { + "path-parse": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", + "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", + "dev": true + }, "resolve": { - "version": "1.7.1", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.7.1.tgz", - "integrity": "sha512-c7rwLofp8g1U+h1KNyHL/jicrKg1Ek4q+Lr33AL65uZTinUZHe30D5HlyN5V9NW0JX1D5dXQ4jqW5l7Sy/kGfw==", + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.10.0.tgz", + "integrity": "sha512-3sUr9aq5OfSg2S9pNtPA9hL1FVEAjvfOC4leW0SNf/mpnaakz2a9femSd6LqAww2RaFctwyf1lCqnTHuF1rxDg==", "dev": true, "requires": { - "path-parse": "^1.0.5" + "path-parse": "^1.0.6" } } } }, "eslint-module-utils": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/eslint-module-utils/-/eslint-module-utils-2.2.0.tgz", - "integrity": "sha1-snA2LNiLGkitMIl2zn+lTphBF0Y=", + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/eslint-module-utils/-/eslint-module-utils-2.3.0.tgz", + "integrity": "sha512-lmDJgeOOjk8hObTysjqH7wyMi+nsHwwvfBykwfhjR1LNdd7C2uFJBvx4OpWYpXOw4df1yE1cDEVd1yLHitk34w==", "dev": true, "requires": { "debug": "^2.6.8", - "pkg-dir": "^1.0.0" - }, - "dependencies": { - "find-up": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-1.1.2.tgz", - "integrity": "sha1-ay6YIrGizgpgq2TWEOzK1TyyTQ8=", - "dev": true, - "requires": { - "path-exists": "^2.0.0", - "pinkie-promise": "^2.0.0" - } - }, - "path-exists": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-2.1.0.tgz", - "integrity": "sha1-D+tsZPD8UY2adU3V77YscCJ2H0s=", - "dev": true, - "requires": { - "pinkie-promise": "^2.0.0" - } - }, - "pkg-dir": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-1.0.0.tgz", - "integrity": "sha1-ektQio1bstYp1EcFb/TpyTFM89Q=", - "dev": true, - "requires": { - "find-up": "^1.0.0" - } - } + "pkg-dir": "^2.0.0" } }, "eslint-plugin-import": { - "version": "2.12.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-import/-/eslint-plugin-import-2.12.0.tgz", - "integrity": "sha1-2tMXgSktZmSyUxf9BJ0uKy8CIF0=", + "version": "2.16.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-import/-/eslint-plugin-import-2.16.0.tgz", + "integrity": "sha512-z6oqWlf1x5GkHIFgrSvtmudnqM6Q60KM4KvpWi5ubonMjycLjndvd5+8VAZIsTlHC03djdgJuyKG6XO577px6A==", "dev": true, "requires": { "contains-path": "^0.1.0", - "debug": "^2.6.8", + "debug": "^2.6.9", "doctrine": "1.5.0", - "eslint-import-resolver-node": "^0.3.1", - "eslint-module-utils": "^2.2.0", - "has": "^1.0.1", - "lodash": "^4.17.4", - "minimatch": "^3.0.3", + "eslint-import-resolver-node": "^0.3.2", + "eslint-module-utils": "^2.3.0", + "has": "^1.0.3", + "lodash": "^4.17.11", + "minimatch": "^3.0.4", "read-pkg-up": "^2.0.0", - "resolve": "^1.6.0" + "resolve": "^1.9.0" }, "dependencies": { "doctrine": { @@ -1652,6 +1630,15 @@ "isarray": "^1.0.0" } }, + "has": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", + "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", + "dev": true, + "requires": { + "function-bind": "^1.1.1" + } + }, "load-json-file": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-2.0.0.tgz", @@ -1664,6 +1651,12 @@ "strip-bom": "^3.0.0" } }, + "path-parse": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", + "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", + "dev": true + }, "path-type": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/path-type/-/path-type-2.0.0.tgz", @@ -1695,12 +1688,12 @@ } }, "resolve": { - "version": "1.7.1", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.7.1.tgz", - "integrity": "sha512-c7rwLofp8g1U+h1KNyHL/jicrKg1Ek4q+Lr33AL65uZTinUZHe30D5HlyN5V9NW0JX1D5dXQ4jqW5l7Sy/kGfw==", + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.10.0.tgz", + "integrity": "sha512-3sUr9aq5OfSg2S9pNtPA9hL1FVEAjvfOC4leW0SNf/mpnaakz2a9femSd6LqAww2RaFctwyf1lCqnTHuF1rxDg==", "dev": true, "requires": { - "path-parse": "^1.0.5" + "path-parse": "^1.0.6" } }, "strip-bom": { @@ -2063,8 +2056,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -2085,14 +2077,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2107,20 +2097,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -2237,8 +2224,7 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -2250,7 +2236,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2265,7 +2250,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2273,14 +2257,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -2299,7 +2281,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -2380,8 +2361,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -2393,7 +2373,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -2479,8 +2458,7 @@ "safe-buffer": { "version": "5.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -2516,7 +2494,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -2536,7 +2513,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -2580,14 +2556,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, @@ -2755,6 +2729,12 @@ "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", "dev": true }, + "has-symbols": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.0.tgz", + "integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q=", + "dev": true + }, "has-value": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/has-value/-/has-value-1.0.0.tgz", @@ -3905,7 +3885,8 @@ "lodash": { "version": "4.17.11", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", - "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" + "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==", + "dev": true }, "lodash.sortby": { "version": "4.7.0", @@ -4254,6 +4235,41 @@ } } }, + "object.assign": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.0.tgz", + "integrity": "sha512-exHJeq6kBKj58mqGyTQ9DFvrZC/eR6OwxzoM9YRoGBqrXYonaFyGiFMuc9VZrXf7DarreEwMpurG3dd+CNyW5w==", + "dev": true, + "requires": { + "define-properties": "^1.1.2", + "function-bind": "^1.1.1", + "has-symbols": "^1.0.0", + "object-keys": "^1.0.11" + } + }, + "object.entries": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/object.entries/-/object.entries-1.1.0.tgz", + "integrity": "sha512-l+H6EQ8qzGRxbkHOd5I/aHRhHDKoQXQ8g0BYt4uSweQU1/J6dZUOyWh9a2Vky35YCKjzmgxOzta2hH6kf9HuXA==", + "dev": true, + "requires": { + "define-properties": "^1.1.3", + "es-abstract": "^1.12.0", + "function-bind": "^1.1.1", + "has": "^1.0.3" + }, + "dependencies": { + "has": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", + "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", + "dev": true, + "requires": { + "function-bind": "^1.1.1" + } + } + } + }, "object.getownpropertydescriptors": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/object.getownpropertydescriptors/-/object.getownpropertydescriptors-2.0.3.tgz", diff --git a/package.json b/package.json index 315585df..d2593672 100644 --- a/package.json +++ b/package.json @@ -3,14 +3,12 @@ "version": "1.1.0", "description": "Data Structures & Algorithms in JS", "main": "./src/index.js", - "dependencies": { - - }, + "dependencies": {}, "devDependencies": { "benchmark": "2.1.4", "eslint": "4.19.1", - "eslint-config-airbnb-base": "12.1.0", - "eslint-plugin-import": "2.12.0", + "eslint-config-airbnb-base": "^13.1.0", + "eslint-plugin-import": "^2.16.0", "eslint-plugin-jest": "21.17.0", "jest": "23.6.0", "textlint-plugin-asciidoctor": "1.0.2" @@ -19,7 +17,8 @@ "test": "jest src/", "watch": "jest src/ --watch --coverage", "coverage": "jest src/ --coverage && open coverage/lcov-report/index.html", - "linter": "npx eslint --fix -f codeframe src/" + "linter": "npx eslint --fix --format codeframe src/", + "ci": "npm run linter && npm test" }, "keywords": [ "algorithms", diff --git a/src/algorithms/sorting/merge-sort.js b/src/algorithms/sorting/merge-sort.js index a4a08882..366f2590 100644 --- a/src/algorithms/sorting/merge-sort.js +++ b/src/algorithms/sorting/merge-sort.js @@ -16,8 +16,8 @@ function merge(array1, array2 = []) { // merge elements on a and b in asc order. Run-time O(a + b) for (let index = 0, i1 = 0, i2 = 0; index < mergedLength; index++) { // <1> - if (i2 >= array2.length || - (i1 < array1.length && array1[i1] <= array2[i2])) { + if (i2 >= array2.length + || (i1 < array1.length && array1[i1] <= array2[i2])) { mergedArray[index] = array1[i1]; // <2> i1 += 1; } else { @@ -45,7 +45,7 @@ function splitSort(array) { // base case if (size < 2) { return array; - } else if (size === 2) { + } if (size === 2) { return array[0] < array[1] ? array : [array[1], array[0]]; // <1> } diff --git a/src/data-structures/custom/lru-cache.js b/src/data-structures/custom/lru-cache.js index 4641a022..13638b6c 100644 --- a/src/data-structures/custom/lru-cache.js +++ b/src/data-structures/custom/lru-cache.js @@ -1,4 +1,3 @@ - /** * Design and implement a data structure for Least Recently Used (LRU) cache. * It should support the following operations: get and put. @@ -31,46 +30,41 @@ * * @param {number} capacity */ -const LRUCache = (capacity) => { - this.map = new Map(); - this.capacity = capacity; -}; +class LRUCache { + constructor(capacity) { + this.map = new Map(); + this.capacity = capacity; + } -/** -* @param {number} key -* @return {number} -*/ -LRUCache.prototype.get = (key) => { - const value = this.map.get(key); - if (value) { - this.moveToTop(key); - return value; + get(key) { + const value = this.map.get(key); + if (value) { + this.moveToTop(key); + return value; + } + return -1; } - return -1; -}; -/** -* @param {number} key -* @param {number} value -* @return {void} -*/ -LRUCache.prototype.put = (key, value) => { - this.map.set(key, value); - this.rotate(key); -}; + put(key, value) { + this.map.set(key, value); + this.rotate(key); + } -LRUCache.prototype.rotate = (key) => { - this.moveToTop(key); - while (this.map.size > this.capacity) { - const it = this.map.keys(); - this.map.delete(it.next().value); + rotate(key) { + this.moveToTop(key); + while (this.map.size > this.capacity) { + const it = this.map.keys(); + this.map.delete(it.next().value); + } } -}; -LRUCache.prototype.moveToTop = (key) => { - if (this.map.has(key)) { - const value = this.map.get(key); - this.map.delete(key); - this.map.set(key, value); + moveToTop(key) { + if (this.map.has(key)) { + const value = this.map.get(key); + this.map.delete(key); + this.map.set(key, value); + } } -}; +} + +module.exports = LRUCache; diff --git a/src/data-structures/linked-lists/linked-list.js b/src/data-structures/linked-lists/linked-list.js index 8cee4abe..b9440791 100644 --- a/src/data-structures/linked-lists/linked-list.js +++ b/src/data-structures/linked-lists/linked-list.js @@ -1,5 +1,5 @@ -const Node = require('./node'); const util = require('util'); +const Node = require('./node'); // tag::constructor[] /** diff --git a/src/data-structures/maps/hash-maps/hash-map-1.js b/src/data-structures/maps/hash-maps/hash-map-1.js index c66109df..4deec02c 100644 --- a/src/data-structures/maps/hash-maps/hash-map-1.js +++ b/src/data-structures/maps/hash-maps/hash-map-1.js @@ -74,4 +74,3 @@ module.exports = HashMap; // assert.equal(hashMap.get('cat'), 8); // got overwritten by art 😱 // assert.equal(hashMap.get('rat'), 8); // got overwritten by art 😱 // assert.equal(hashMap.get('dog'), 8); // got overwritten by art 😱 - diff --git a/src/data-structures/queues/queue.js b/src/data-structures/queues/queue.js index 60d38178..2ef458a8 100644 --- a/src/data-structures/queues/queue.js +++ b/src/data-structures/queues/queue.js @@ -32,6 +32,7 @@ class Queue { dequeue() { return this.items.removeFirst(); } + // end::dequeue[] /** * Size of the queue @@ -66,4 +67,3 @@ queue.dequeue(); //↪️ b queue.dequeue(); //↪️ c // end::snippet[] // */ - diff --git a/src/data-structures/sets/array-set.js b/src/data-structures/sets/array-set.js index 8c11d069..4505c891 100644 --- a/src/data-structures/sets/array-set.js +++ b/src/data-structures/sets/array-set.js @@ -85,4 +85,3 @@ class ArraySet { } module.exports = ArraySet; - diff --git a/src/data-structures/sets/hash-set.js b/src/data-structures/sets/hash-set.js index 3327745b..4f139204 100644 --- a/src/data-structures/sets/hash-set.js +++ b/src/data-structures/sets/hash-set.js @@ -87,4 +87,3 @@ class HashMapSet { } module.exports = HashMapSet; - diff --git a/src/data-structures/sets/map-set.js b/src/data-structures/sets/map-set.js index b1c53485..f261892d 100644 --- a/src/data-structures/sets/map-set.js +++ b/src/data-structures/sets/map-set.js @@ -72,4 +72,3 @@ class MapSet { } module.exports = MapSet; - diff --git a/src/data-structures/stacks/stack-1.js b/src/data-structures/stacks/stack-1.js index c9022de8..51169097 100644 --- a/src/data-structures/stacks/stack-1.js +++ b/src/data-structures/stacks/stack-1.js @@ -37,4 +37,3 @@ Stack.prototype.push = Stack.prototype.add; Stack.prototype.pop = Stack.prototype.remove; module.exports = Stack; - diff --git a/src/data-structures/stacks/stack.js b/src/data-structures/stacks/stack.js index 9ab5a357..e96a6807 100644 --- a/src/data-structures/stacks/stack.js +++ b/src/data-structures/stacks/stack.js @@ -67,4 +67,3 @@ stack.remove(); //↪️ c stack.remove(); //↪️ a // end::snippet[] // */ - diff --git a/src/data-structures/trees/avl-tree.js b/src/data-structures/trees/avl-tree.js index fc6379b1..573095bd 100644 --- a/src/data-structures/trees/avl-tree.js +++ b/src/data-structures/trees/avl-tree.js @@ -24,14 +24,14 @@ function balance(node) { // left subtree is higher than right subtree if (node.left.balanceFactor > 0) { return rightRotation(node); - } else if (node.left.balanceFactor < 0) { + } if (node.left.balanceFactor < 0) { return leftRightRotation(node); } } else if (node.balanceFactor < -1) { // right subtree is higher than left subtree if (node.right.balanceFactor < 0) { return leftRotation(node); - } else if (node.right.balanceFactor > 0) { + } if (node.right.balanceFactor > 0) { return rightLeftRotation(node); } } diff --git a/src/data-structures/trees/binary-search-tree.js b/src/data-structures/trees/binary-search-tree.js index 12b562bc..43900c54 100644 --- a/src/data-structures/trees/binary-search-tree.js +++ b/src/data-structures/trees/binary-search-tree.js @@ -69,7 +69,7 @@ class BinarySearchTree { findNodeAndParent(value, node = this.root, parent = null) { if (!node || node.value === value) { return { found: node, parent }; - } else if (value < node.value) { + } if (value < node.value) { return this.findNodeAndParent(value, node.left, node); } return this.findNodeAndParent(value, node.right, node); diff --git a/src/runtimes/02-binary-search.js b/src/runtimes/02-binary-search.js index c094f78a..48783459 100644 --- a/src/runtimes/02-binary-search.js +++ b/src/runtimes/02-binary-search.js @@ -18,9 +18,9 @@ function binarySearchRecursive(array, search, offset = 0) { if (current === search) { return offset + half; - } else if (array.length < 2) { + } if (array.length < 2) { return -1; - } else if (search > current) { + } if (search > current) { const right = array.slice(half); return binarySearchRecursive(right, search, offset + half); } @@ -49,7 +49,7 @@ function binarySearchIterative(array, search) { if (current === search) { return currentIndex; - } else if (search > current) { + } if (search > current) { start = currentIndex; } else if (search < current) { end = currentIndex;