Last active
March 30, 2021 12:37
-
-
Save railsstudent/4cdbc260fedea8dbbfe78c2c59bee4d5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth) { | |
| //your code here | |
| if (startPriceOld >= startPriceNew) { | |
| return [0, Math.floor(startPriceOld - startPriceNew)]; | |
| } | |
| let months = 0; | |
| let totalSaving = 0; | |
| let depreciatedPriceNew = startPriceNew; | |
| let depreciatedPriceOld = startPriceOld; | |
| let lossPercentage = percentLossByMonth; | |
| while ((totalSaving + depreciatedPriceOld) < depreciatedPriceNew) { | |
| months += 1; | |
| if (months % 2 === 0) { | |
| lossPercentage += 0.5; | |
| } | |
| totalSaving += savingperMonth; | |
| depreciatedPriceOld -= depreciatedPriceOld * (lossPercentage / 100); | |
| depreciatedPriceNew -= depreciatedPriceNew * (lossPercentage / 100); | |
| } | |
| return [months, Math.round(totalSaving + depreciatedPriceOld - depreciatedPriceNew)]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment