Skip to content

Commit 7b4ff6e

Browse files
Add checks for invalid inputs
1 parent f8dc85c commit 7b4ff6e

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

Maths/ExponentialFunction.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ let factorial = 1
55
* @description Calculates the n+1 th order Taylor series approximation of exponential function e^x given n
66
* @param {Integer} power
77
* @param {Integer} order - 1
8-
* @returns exponentialFunction(18,4) = 18.4
8+
* @returns exponentialFunction(2,20) = 7.389056098930604
99
* @url https://en.wikipedia.org/wiki/Exponential_function
1010
*/
1111
function exponentialFunction (power, n) {
12+
if (isNaN(power) || isNaN(n) || n < 0) {
13+
throw new TypeError('Invalid Input')
14+
}
1215
if (n === 0) { return 1 }
1316
const recursion = exponentialFunction(power, n - 1)
1417
powerOfX = powerOfX * power

Maths/test/ExponentialFunction.test.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { exponentialFunction } from '../ExponentialFunction'
22

3-
describe('exponentialFunction', () => {
4-
it('with a power of 5 and order of 21', () => {
3+
describe('Tests for exponential function', () => {
4+
it('should be a function', () => {
5+
expect(typeof exponentialFunction).toEqual('function')
6+
})
7+
8+
it('should throw error for invalid input', () => {
9+
expect(() => exponentialFunction(2, -34)).toThrow()
10+
})
11+
12+
it('should return the exponential function of power of 5 and order of 21', () => {
513
const ex = exponentialFunction(5, 20)
614
expect(ex).toBe(148.4131470673818)
715
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy