'Get return on investment (ROI) from two numbers in Javascript?

I'm building an app that tracks stock predictions.

I would like to have a function that takes in two numbers, and calculate the return on investment between the two numbers.

For example with a start price of $50, and a current price of $100, the function should return "100", since the price has increased 100% since the first purchase. With a start price of $100, and a current price of $10, the function should return "-90", since that investment would have lost 90% of its value



Solution 1:[1]

This seems more like a math question than a programming question, but here goes:

function roi(cost, profit) {
    return (profit - cost) / cost * 100;
}

Solution 2:[2]

function getROI(cost, profit) {
  return (profit - cost) / cost;
}

Solution 3:[3]

let startPrice = 50
let endPrice = 100

let ROI = 100 * ((endPrice/startPrice) - 1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Copper
Solution 2 Samuel Beaulieu
Solution 3 Mike