Skip to content

Instantly share code, notes, and snippets.

@mojenmojen
Created September 24, 2016 03:51
Show Gist options
  • Save mojenmojen/469624e00023b398f8d22a50d9978ad5 to your computer and use it in GitHub Desktop.
Save mojenmojen/469624e00023b398f8d22a50d9978ad5 to your computer and use it in GitHub Desktop.
Codewars Kata: Mr. Scrooge has a sum of money 'P' that wants to invest, and he wants to know how many years 'Y' this sum has to be kept in the bank in order for this sum of money to amount to 'D'. The sum is kept for 'Y' years in the bank where interest 'I' is paid yearly, and the new sum is re-invested yearly after paying tax 'T' Note that the …
function calculateYears(principal, interest, tax, desired) {
// create an endless loop that will increment the number of years
for ( var year = 0;; year++ ) {
// check if the principal has reached the desired amount
if ( principal >= desired ) {
return year;
}
// calculate the interest for this year
var currentYearInterest = interest * principal;
// calculate the tax on the interest for this year
var currentYearTax = currentYearInterest * tax;
// adjust the principal to add the interest and minus the tax
principal = principal + currentYearInterest - currentYearTax;
}
}
@ldco2016
Copy link

I am curious to know how does this pass using two semicolons after var year = 0?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment