diff --git a/src/algorithms/math/bits/README.md b/src/algorithms/math/bits/README.md index 736cbebf..05dadaba 100644 --- a/src/algorithms/math/bits/README.md +++ b/src/algorithms/math/bits/README.md @@ -2,12 +2,7 @@ #### Get Bit -This method shifts `1` over by `bitPosition` bits, creating a -value that looks like `00100`. Then we perform `AND` operation -that clears all bits from the original number except the -`bitPosition` one. Then we compare the result with zero. If -result is zero that would mean that original number has `0` at -position `bitPosition`. +This method shifts the relevant bit to the zeroth position. Then we perform 'AND' operation with one which has bit pattern like '00001'. This clears all bits from the original number except the relevant one. If the relevant bit is one, the result is '1', otherwise the result is '0'. > See `getBit` function for further details. diff --git a/src/algorithms/math/bits/getBit.js b/src/algorithms/math/bits/getBit.js index a99ce7d5..5ff21326 100644 --- a/src/algorithms/math/bits/getBit.js +++ b/src/algorithms/math/bits/getBit.js @@ -4,5 +4,5 @@ * @return {number} */ export default function getBit(number, bitPosition) { - return (number & (1 << bitPosition)) === 0 ? 0 : 1; + return (number >> bitPosition) & 1; } diff --git a/src/algorithms/math/euclidean-algorithm/__test__/euclieanAlgorithm.test.js b/src/algorithms/math/euclidean-algorithm/__test__/euclieanAlgorithm.test.js index 01d4f4e1..ab2e3d6c 100644 --- a/src/algorithms/math/euclidean-algorithm/__test__/euclieanAlgorithm.test.js +++ b/src/algorithms/math/euclidean-algorithm/__test__/euclieanAlgorithm.test.js @@ -2,7 +2,7 @@ import euclideanAlgorithm from '../euclideanAlgorithm'; describe('euclideanAlgorithm', () => { it('should calculate GCD', () => { - expect(euclideanAlgorithm(0, 0)).toBeNull(); + expect(euclideanAlgorithm(0, 0)).toBe(0); expect(euclideanAlgorithm(2, 0)).toBe(2); expect(euclideanAlgorithm(0, 2)).toBe(2); expect(euclideanAlgorithm(1, 2)).toBe(1); diff --git a/src/algorithms/math/euclidean-algorithm/euclideanAlgorithm.js b/src/algorithms/math/euclidean-algorithm/euclideanAlgorithm.js index 1532ea35..fe3b57dd 100644 --- a/src/algorithms/math/euclidean-algorithm/euclideanAlgorithm.js +++ b/src/algorithms/math/euclidean-algorithm/euclideanAlgorithm.js @@ -1,32 +1,11 @@ /** * @param {number} originalA * @param {number} originalB - * @return {number|null} + * @return {number} */ export default function euclideanAlgorithm(originalA, originalB) { const a = Math.abs(originalA); const b = Math.abs(originalB); - if (a === 0 && b === 0) { - return null; - } - - if (a === 0 && b !== 0) { - return b; - } - - if (a !== 0 && b === 0) { - return a; - } - - // Normally we need to do subtraction (a - b) but to prevent - // recursion occurs to often we may shorten subtraction to (a % b). - // Since (a % b) is normally means that we've subtracted b from a - // many times until the difference became less then a. - - if (a > b) { - return euclideanAlgorithm(a % b, b); - } - - return euclideanAlgorithm(b % a, a); + return (b === 0) ? a : euclideanAlgorithm(b, a % b); } diff --git a/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwo.test.js b/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwo.test.js index 69058943..f5fb4fed 100644 --- a/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwo.test.js +++ b/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwo.test.js @@ -1,17 +1,10 @@ import isPowerOfTwo from '../isPowerOfTwo'; describe('isPowerOfTwo', () => { - it('should throw an exception when trying to apply function to negative number', () => { - const isNegativePowerOfTwo = () => { - isPowerOfTwo(-1); - }; - - expect(isNegativePowerOfTwo).toThrowError(); - }); - it('should check if the number is made by multiplying twos', () => { + expect(isPowerOfTwo(-1)).toBeFalsy(); expect(isPowerOfTwo(0)).toBeFalsy(); - expect(isPowerOfTwo(1)).toBeFalsy(); + expect(isPowerOfTwo(1)).toBeTruthy(); expect(isPowerOfTwo(2)).toBeTruthy(); expect(isPowerOfTwo(3)).toBeFalsy(); expect(isPowerOfTwo(4)).toBeTruthy(); diff --git a/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwoBitwise.test.js b/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwoBitwise.test.js index 9f6ef06e..e092798d 100644 --- a/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwoBitwise.test.js +++ b/src/algorithms/math/is-power-of-two/__test__/isPowerOfTwoBitwise.test.js @@ -1,17 +1,10 @@ import isPowerOfTwoBitwise from '../isPowerOfTwoBitwise'; describe('isPowerOfTwoBitwise', () => { - it('should throw an exception when trying to apply function to negative number', () => { - const isNegativePowerOfTwo = () => { - isPowerOfTwoBitwise(-1); - }; - - expect(isNegativePowerOfTwo).toThrowError(); - }); - it('should check if the number is made by multiplying twos', () => { + expect(isPowerOfTwoBitwise(-1)).toBeFalsy(); expect(isPowerOfTwoBitwise(0)).toBeFalsy(); - expect(isPowerOfTwoBitwise(1)).toBeFalsy(); + expect(isPowerOfTwoBitwise(1)).toBeTruthy(); expect(isPowerOfTwoBitwise(2)).toBeTruthy(); expect(isPowerOfTwoBitwise(3)).toBeFalsy(); expect(isPowerOfTwoBitwise(4)).toBeTruthy(); diff --git a/src/algorithms/math/is-power-of-two/isPowerOfTwo.js b/src/algorithms/math/is-power-of-two/isPowerOfTwo.js index 0663b4ee..6f7590dd 100644 --- a/src/algorithms/math/is-power-of-two/isPowerOfTwo.js +++ b/src/algorithms/math/is-power-of-two/isPowerOfTwo.js @@ -3,13 +3,8 @@ * @return {boolean} */ export default function isPowerOfTwo(number) { - // Don't work with negative numbers. - if (number < 0) { - throw new Error('Please provide positive number'); - } - - // 0 and 1 are not powers of two. - if (number <= 1) { + // 1 (2^0) is the smallest power of two. + if (number < 1) { return false; } diff --git a/src/algorithms/math/is-power-of-two/isPowerOfTwoBitwise.js b/src/algorithms/math/is-power-of-two/isPowerOfTwoBitwise.js index 1433b825..af117735 100644 --- a/src/algorithms/math/is-power-of-two/isPowerOfTwoBitwise.js +++ b/src/algorithms/math/is-power-of-two/isPowerOfTwoBitwise.js @@ -3,13 +3,8 @@ * @return {boolean} */ export default function isPowerOfTwoBitwise(number) { - // Don't work with negative numbers. - if (number < 0) { - throw new Error('Please provide positive number'); - } - - // 0 and 1 are not powers of two. - if (number <= 1) { + // 1 (2^0) is the smallest power of two. + if (number < 1) { return false; } diff --git a/src/algorithms/math/least-common-multiple/leastCommonMultiple.js b/src/algorithms/math/least-common-multiple/leastCommonMultiple.js index e40f9c95..26a76810 100644 --- a/src/algorithms/math/least-common-multiple/leastCommonMultiple.js +++ b/src/algorithms/math/least-common-multiple/leastCommonMultiple.js @@ -7,9 +7,5 @@ import euclideanAlgorithm from '../euclidean-algorithm/euclideanAlgorithm'; */ export default function leastCommonMultiple(a, b) { - if (a === 0 && b === 0) { - return 0; - } - - return Math.abs(a * b) / euclideanAlgorithm(a, b); + return ((a === 0) || (b === 0)) ? 0 : Math.abs(a * b) / euclideanAlgorithm(a, b); } diff --git a/src/algorithms/sets/fisher-yates/fisherYates.js b/src/algorithms/sets/fisher-yates/fisherYates.js index 98cb2c31..23b710b9 100644 --- a/src/algorithms/sets/fisher-yates/fisherYates.js +++ b/src/algorithms/sets/fisher-yates/fisherYates.js @@ -6,15 +6,9 @@ export default function fisherYates(originalArray) { // Clone array from preventing original array from modification (for testing purpose). const array = originalArray.slice(0); - if (array.length <= 1) { - return array; - } - for (let i = (array.length - 1); i > 0; i -= 1) { const randomIndex = Math.floor(Math.random() * (i + 1)); - const tmp = array[randomIndex]; - array[randomIndex] = array[i]; - array[i] = tmp; + [array[i], array[randomIndex]] = [array[randomIndex], array[i]]; } return array;