Skip to content

Commit ad50df5

Browse files
Add return on investment to maths
1 parent 19b4ced commit ad50df5

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

maths/return_on_investment.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @function returnOnInvestment
3+
* @description Calculates Return on Investment (ROI) as a percentage.
4+
* ROI measures the profitability of an investment relative to its cost.
5+
* Formula: ROI = (Gain - Cost) / Cost * 100
6+
* @param {number} gainFromInvestment - Total value gained from the investment
7+
* @param {number} costOfInvestment - Total cost of the investment
8+
* @return {number} ROI as a percentage
9+
* @see https://www.investopedia.com/terms/r/returnoninvestment.asp
10+
* @example returnOnInvestment(1000, 500) = 100
11+
* @example returnOnInvestment(500, 500) = 0
12+
* @example returnOnInvestment(200, 500) = -60
13+
*/
14+
export const returnOnInvestment = (
15+
gainFromInvestment: number,
16+
costOfInvestment: number
17+
): number => {
18+
if (costOfInvestment <= 0) {
19+
throw new RangeError('costOfInvestment must be greater than 0')
20+
}
21+
return ((gainFromInvestment - costOfInvestment) / costOfInvestment) * 100
22+
}

0 commit comments

Comments
 (0)