Skip to content

Commit 2ebe65b

Browse files
Add Pow.js (TheAlgorithms#498)
* Add pow.js * Add tests for Pow.js
1 parent bb1a5fb commit 2ebe65b

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Maths/Pow.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Returns the value of x to the power of y
2+
3+
const pow = (x, y) => {
4+
let result = 1
5+
for (let i = 1; i <= y; i++) {
6+
result *= x
7+
}
8+
return result
9+
}
10+
11+
export { pow }

Maths/test/Pow.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { pow } from '../Pow'
2+
3+
describe('Pow', () => {
4+
it('should return 1 for numbers with exponent 0', () => {
5+
expect(pow(2, 0)).toBe(1)
6+
})
7+
8+
it('should return 0 for numbers with base 0', () => {
9+
expect(pow(0, 23)).toBe(0)
10+
})
11+
12+
it('should return the base to the exponent power', () => {
13+
expect(pow(24, 4)).toBe(331776)
14+
})
15+
})

0 commit comments

Comments
 (0)