From 5e6d813b6f1ec0741c07a713c2b3d3ea9b9ec3ff Mon Sep 17 00:00:00 2001 From: Rahul Jain Date: Thu, 29 Oct 2020 21:54:07 +0530 Subject: [PATCH] Update Binary Exponentiation algorithm file to comply with Javascript standard rules --- Maths/BinaryExponentiationIterative.js | 16 +++++++-------- .../BinaryExponentiationIterative.test.js | 20 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Maths/BinaryExponentiationIterative.js b/Maths/BinaryExponentiationIterative.js index 6a8bb77b0..705dcded4 100644 --- a/Maths/BinaryExponentiationIterative.js +++ b/Maths/BinaryExponentiationIterative.js @@ -12,13 +12,13 @@ // = exponent(x*x, floor(n/2)) ; if n is odd // = x*exponent(x*x, floor(n/2)) ; if n is even const exponent = (x, n) => { - let ans = 1 - while(n > 0) { - if(n%2 != 0) ans *= x - n = Math.floor(n/2) - if(n > 0) x *= x - } - return ans + let ans = 1 + while (n > 0) { + if (n % 2 !== 0) ans *= x + n = Math.floor(n / 2) + if (n > 0) x *= x + } + return ans } -export { exponent } \ No newline at end of file +export { exponent } diff --git a/Maths/test/BinaryExponentiationIterative.test.js b/Maths/test/BinaryExponentiationIterative.test.js index ebd21cbac..6e6fff8bd 100644 --- a/Maths/test/BinaryExponentiationIterative.test.js +++ b/Maths/test/BinaryExponentiationIterative.test.js @@ -1,15 +1,15 @@ import { exponent } from '../BinaryExponentiationIterative' describe('exponent', () => { - it('should return 1 when power is 0', () => { - expect(exponent(5, 0)).toBe(1) - }) + it('should return 1 when power is 0', () => { + expect(exponent(5, 0)).toBe(1) + }) - it('should return 0 when base is 0', () => { - expect(exponent(0, 7)).toBe(0) - }) + it('should return 0 when base is 0', () => { + expect(exponent(0, 7)).toBe(0) + }) - it('should return the value of a base raised to a power', () => { - expect(exponent(3, 5)).toBe(243) - }) -}) \ No newline at end of file + it('should return the value of a base raised to a power', () => { + expect(exponent(3, 5)).toBe(243) + }) +})