Search code examples
javascriptweb-applicationssveltecalculation

How to calculate a return of investment with inflation added?


I'd like to ask someone who is more skillful than me. I need to calculate a return on investment but I need to add inflation for each year.

Example: I made a purchase of some assets that generate/save money (solar power/car for job/ tools). Purchasing price of that asset was 20 000$ and generates 2350$ each year. In the simple model, the asset is paid off in 8,5 years (20000/2350) but it doesn't count with inflation which can be 3% each year on average. I tried to find a solution but I was able to figure it out only for the first year because for the second year the calculation is not from 2350$ but from 2420$ (2350 + 3%) and so on... the desired result is to calculate years of the inflation-adjusted return.

My effort so far:

<script>
    let price = 20000;
    let gain_year = 2350; 
    let inflation = 3;
    
 $: return_in_years = price /gain_year;
    
// this is only for the first year
// $: inflation_calc = (gain_year/100)*inflation;
    
</script>

<label for="gain_year">Gain in one year</label>
<input id="gain_year" type="number" bind:value={gain_year} />

<label for="price">Price</label>
<input id="price" type="number" bind:value={price} />

<label for="inflation">Inflation</label>
<input id="inflation" type="number" bind:value={inflation} />

<div>
  Return in how many years: {return_in_years.toFixed(1)}
</div>

Thank you guys for your time.


Solution

  • I am not an expert in finance but I think this might work for you. Please apply only the concept here. Your actual code will look different from this.

    // your inputs
    let price = 20000;
    let gain_year = 2350; 
    let inflation = 3;
    // result in years
    let years = 0;
    // loop for each year until the invesment is returned
    while (price > 0){
      // apply inflation except the first year
      if (years > 0) gain_year += gain_year * inflation/100;
      // price is more than gain this year
      if (price >= gain_year){
        // update full year and price according to the gain this year
        years++;
        price -= gain_year;
      }
      // price is less than gain this year
      else {
        // update year according to the portion of the price and gain this year
        years += price / gain_year;
        // set price to 0 so it ends
        price = 0;
      }
      // result
      console.log(price, years, gain_year);
    }