From 9c35031e31c67d9941f982b2c31224034a8e248a Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Fri, 8 Nov 2019 17:28:42 +0530 Subject: [PATCH] fix: linter fixes --- package.json | 1 + .../max-product-of-3-numbers/index.js | 1 + .../max-product-of-3-numbers.test.js | 185 ++++++++++++++++-- .../next-greater-element.test.js | 14 +- src/_Problems_/product-of-elements/index.js | 6 +- src/_Problems_/reverse-number/index.js | 13 +- .../BinarySearch/BinarySearch.test.js | 1 - src/_Searching_/BinarySearch/index.js | 12 +- 8 files changed, 203 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 5dd70a80..3642f1cd 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "", "main": "index.js", "scripts": { + "lint": "./node_modules/.bin/eslint ./src", "coverage": "jest --coverage", "coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls", "test": "jest --verbose", diff --git a/src/_Problems_/max-product-of-3-numbers/index.js b/src/_Problems_/max-product-of-3-numbers/index.js index 3e967083..7f480013 100644 --- a/src/_Problems_/max-product-of-3-numbers/index.js +++ b/src/_Problems_/max-product-of-3-numbers/index.js @@ -34,6 +34,7 @@ function maxProductof3NumbersII(arr) { throw new Error('Invalid Argument'); } + // eslint-disable-next-line no-multi-assign let firstMax = (secondMax = thirdMax = Number.MIN_SAFE_INTEGER); let firstMin = (secondMin = Number.MAX_SAFE_INTEGER); diff --git a/src/_Problems_/max-product-of-3-numbers/max-product-of-3-numbers.test.js b/src/_Problems_/max-product-of-3-numbers/max-product-of-3-numbers.test.js index fd29122e..90e66461 100644 --- a/src/_Problems_/max-product-of-3-numbers/max-product-of-3-numbers.test.js +++ b/src/_Problems_/max-product-of-3-numbers/max-product-of-3-numbers.test.js @@ -1,37 +1,200 @@ -const { maxProductof3Numbers, maxProductof3NumbersII } = require("."); +const { maxProductof3Numbers, maxProductof3NumbersII } = require('.'); -describe("Maximum Product of three numbers", () => { - it("throws an error with no Array is passed", () => { +describe('Maximum Product of three numbers', () => { + it('throws an error with no Array is passed', () => { expect(() => { - maxProductof3Numbers("xunda"); + maxProductof3Numbers('xunda'); }).toThrowError(); expect(() => { - maxProductof3NumbersII("xunda"); + maxProductof3NumbersII('xunda'); }).toThrowError(); }); - it("returns the product of an array with 3 numbers", () => { + it('returns the product of an array with 3 numbers', () => { expect(maxProductof3Numbers([1, 2, 3])).toEqual(6); expect(maxProductof3NumbersII([1, 2, 3])).toEqual(6); }); - it("returns the product of an array with positive and negative numbers", () => { + it('returns the product of an array with positive and negative numbers', () => { expect(maxProductof3Numbers([-10, -10, 2, 3])).toEqual(300); expect(maxProductof3NumbersII([-10, -10, 2, 3])).toEqual(300); }); - it("returns the product of an array with negative numbers", () => { + it('returns the product of an array with negative numbers', () => { expect(maxProductof3Numbers([-10, -1, -2, -10])).toEqual(-20); expect(maxProductof3NumbersII([-10, -1, -2, -10])).toEqual(-20); }); - it("returns the proper calculation if the array is large", () => { - const largeArray = [100, 100, 100, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 45, 4, 3, 7, 8, 1, 3, 7, 8, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 45, 4, 3, 7, 8, 1, 3, 7, 8]; + it('returns the proper calculation if the array is large', () => { + const largeArray = [ + 100, + 100, + 100, + 12, + 3, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 12, + 3, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 12, + 3, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 3, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 12, + 3, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 12, + 3, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + ]; expect(maxProductof3Numbers(largeArray)).toEqual(100 * 100 * 100); expect(maxProductof3NumbersII(largeArray)).toEqual(100 * 100 * 100); }); - it("returns an error if there are less than 3 numbers", () => { + it('returns an error if there are less than 3 numbers', () => { expect(() => { maxProductof3Numbers([-10, -1]); }).toThrowError(); diff --git a/src/_Problems_/next-greater-element/next-greater-element.test.js b/src/_Problems_/next-greater-element/next-greater-element.test.js index e729f0db..474d0315 100644 --- a/src/_Problems_/next-greater-element/next-greater-element.test.js +++ b/src/_Problems_/next-greater-element/next-greater-element.test.js @@ -2,8 +2,8 @@ const { nextGreaterElement } = require('.'); describe('Next greater element', () => { it('returns next greater elements collection', () => { - const input = [4, 6, 3, 2, 8, 1] - const greaterElements = [6, 8, 8, 8, -1, -1] + const input = [4, 6, 3, 2, 8, 1]; + const greaterElements = [6, 8, 8, 8, -1, -1]; expect(nextGreaterElement(input)).toEqual(greaterElements); }); @@ -17,22 +17,22 @@ describe('Next greater element', () => { }); it('returns a collection of -1 if there is no greater element', () => { - const input = [90, 40, 15, 7, -1, -10] - const greaterElements = [-1, -1, -1, -1, -1, -1] + const input = [90, 40, 15, 7, -1, -10]; + const greaterElements = [-1, -1, -1, -1, -1, -1]; expect(nextGreaterElement(input)).toEqual(greaterElements); }); it('uses -1 if the numbers are the same', () => { - const input = [90, 90] - const greaterElements = [-1, -1] + const input = [90, 90]; + const greaterElements = [-1, -1]; expect(nextGreaterElement(input)).toEqual(greaterElements); }); it('throws an error if the input is not an array', () => { expect(() => { - nextGreaterElement('xunda') + nextGreaterElement('xunda'); }).toThrowError('Invalid Argument'); }); }); diff --git a/src/_Problems_/product-of-elements/index.js b/src/_Problems_/product-of-elements/index.js index 884458b5..27d079a1 100644 --- a/src/_Problems_/product-of-elements/index.js +++ b/src/_Problems_/product-of-elements/index.js @@ -1,4 +1,5 @@ -// An array such that each index has a product of all the numbers in the array except the number stored at that index. +// An array such that each index has a product of all the numbers +// in the array except the number stored at that index. /** * Input - [1, 2, 3, 4] @@ -13,7 +14,8 @@ function findProduct(arr) { const result = []; // multiply all the numbers to the left side - for (let el of arr) { + // eslint-disable-next-line no-restricted-syntax + for (const el of arr) { result.push(left); left *= el; } diff --git a/src/_Problems_/reverse-number/index.js b/src/_Problems_/reverse-number/index.js index bec47765..9e8256f0 100644 --- a/src/_Problems_/reverse-number/index.js +++ b/src/_Problems_/reverse-number/index.js @@ -16,7 +16,7 @@ function reverseNumber(num) { } /** - * + * * Given a 32-bit signed integer, reverse digits of an integer. Example 1: @@ -32,14 +32,16 @@ function reverseNumber(num) { Input: 1534236469 Output: 0 // overflows Note: - Assume we are dealing with an environment which could only - store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. - For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. + Assume we are dealing with an environment which could only + store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. + For the purpose of this problem, assume that your function returns 0 when + the reversed integer overflows. */ function reverse32BitInt(x) { let isNegetive = 0; if (x < 0) { + // eslint-disable-next-line no-param-reassign x *= -1; isNegetive = 1; } @@ -47,6 +49,7 @@ function reverse32BitInt(x) { while (x >= 1) { const r = Math.floor(x % 10); reverse = reverse * 10 + r; + // eslint-disable-next-line no-param-reassign x = Math.floor(x / 10); } if (reverse > 0x7fffffff) { @@ -57,5 +60,5 @@ function reverse32BitInt(x) { module.exports = { reverseNumber, - reverse32BitInt + reverse32BitInt, }; diff --git a/src/_Searching_/BinarySearch/BinarySearch.test.js b/src/_Searching_/BinarySearch/BinarySearch.test.js index f10faadd..229ffa02 100644 --- a/src/_Searching_/BinarySearch/BinarySearch.test.js +++ b/src/_Searching_/BinarySearch/BinarySearch.test.js @@ -37,5 +37,4 @@ describe('Binary Search', () => { expect(binarySearchRecursive(array, low, high, 10)).toEqual(null); }); }); - }); diff --git a/src/_Searching_/BinarySearch/index.js b/src/_Searching_/BinarySearch/index.js index 89d98f49..9b59eb32 100644 --- a/src/_Searching_/BinarySearch/index.js +++ b/src/_Searching_/BinarySearch/index.js @@ -6,7 +6,7 @@ function binarySearch(arr, key) { let high = arr.length - 1; while (low <= high) { - let mid = Math.floor((low + high) / 2); + const mid = Math.floor((low + high) / 2); if (key < arr[mid]) { high = mid - 1; @@ -20,16 +20,20 @@ function binarySearch(arr, key) { return null; } +// eslint-disable-next-line consistent-return function binarySearchRecursive(arr, low, high, key) { const mid = Math.floor((high - low) / 2 + low); if (high <= low && arr[mid] !== key) { return null; - } else if (key === arr[mid]) { + } + if (key === arr[mid]) { return mid; - } else if (key < arr[mid]) { + } + if (key < arr[mid]) { return binarySearchRecursive(arr, low, mid - 1, key); - } else if (key > arr[mid]) { + } + if (key > arr[mid]) { return binarySearchRecursive(arr, mid + 1, high, key); } }