diff --git a/DIRECTORY.md b/DIRECTORY.md index dccfbb7c9a..8cd38b0042 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -198,6 +198,7 @@ * [ShorsAlgorithm](Maths/ShorsAlgorithm.js) * [SieveOfEratosthenes](Maths/SieveOfEratosthenes.js) * [SimpsonIntegration](Maths/SimpsonIntegration.js) + * [SimpleInterest](Maths/SimpleIntrest.js) * [Softmax](Maths/Softmax.js) * [SquareRoot](Maths/SquareRoot.js) * [SumOfDigits](Maths/SumOfDigits.js) diff --git a/Maths/SimpleInterest.js b/Maths/SimpleInterest.js new file mode 100644 index 0000000000..fbc7622785 --- /dev/null +++ b/Maths/SimpleInterest.js @@ -0,0 +1,15 @@ +/** + * @function SimpleInterest + * @description to calculate the amount of interest charged on a sum at a given rate and for a given period of time + * @param {number} principal - input: the principal amount + * @param {number} interestRate - input: the rate of interest in percentage + * @param {number} time - input: the time period in year + * @return {number} simpleInterest + */ + +const SimpleInterest = (principal, interestRate, time) => { + const simpleInterest = Math.floor((principal * interestRate * time) / 100) + return simpleInterest +} + +export { SimpleInterest } diff --git a/Maths/test/SimpleInterest.test.js b/Maths/test/SimpleInterest.test.js new file mode 100644 index 0000000000..054b740746 --- /dev/null +++ b/Maths/test/SimpleInterest.test.js @@ -0,0 +1,13 @@ +import { SimpleInterest } from '../SimpleInterest' + +describe('Testing Simple Interest Rate calculate function', () => { + it('should 750 for principal = 5000, rate = 5%, time = 3 yrs', () => { + expect(SimpleInterest(5000, 5, 3)).toBe(750) + }) + it('should 750 for principal = 1000, rate = 3%, time = 2 yrs', () => { + expect(SimpleInterest(10000, 3, 2)).toBe(600) + }) + it('should 750 for principal = 40000, rate = 0.1%, time = 10 yrs', () => { + expect(SimpleInterest(40000, 0.1, 10)).toBe(400) + }) +})