JS - Dividend calculator function

0 votes
257 views
added Jul 13, 2018 in Javascript by LC Marshal Captain (25,790 points)

The following function to calculate 5% dividend of RM100 for 5 years

function dividendFunc(value, years = 5) {
  if(years > 0)
    return dividendFunc(value*1.05, years - 1);
  else
  return value;
}

alert('RM' + dividendFunc(100));  //function output
alert('RM' + 100 * 1.05 * 1.05 * 1.05 * 1.05 * 1.05); //to check whether output are correct

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...