Update Binary Exponentiation algorithm file to comply with Javascript standard rules

This commit is contained in:
Rahul Jain
2020-10-29 21:54:07 +05:30
parent c7fe697ca4
commit 5e6d813b6f
2 changed files with 18 additions and 18 deletions

View File

@@ -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 }
export { exponent }

View File

@@ -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)
})
})
it('should return the value of a base raised to a power', () => {
expect(exponent(3, 5)).toBe(243)
})
})