Skip to content

Instantly share code, notes, and snippets.

@railsstudent
Last active March 30, 2021 12:37
Show Gist options
  • Save railsstudent/4cdbc260fedea8dbbfe78c2c59bee4d5 to your computer and use it in GitHub Desktop.
Save railsstudent/4cdbc260fedea8dbbfe78c2c59bee4d5 to your computer and use it in GitHub Desktop.
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