Created
September 24, 2016 03:51
-
-
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 …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am curious to know how does this pass using two semicolons after var year = 0?