-
Notifications
You must be signed in to change notification settings - Fork 0
/
buying_a_car.js
25 lines (22 loc) · 934 Bytes
/
buying_a_car.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth) {
//your code here
let priceOld = startPriceOld
let priceNew = startPriceNew
let monthsCount = 0
let reductionPercentage = percentLossByMonth
let monthlySavings = 0
if (startPriceOld >= startPriceNew) {
return [0, Math.floor(startPriceOld - startPriceNew)]
}
while ((monthlySavings + priceOld) < priceNew) {
monthsCount += 1
monthlySavings += savingperMonth
if (monthsCount % 2 === 0) reductionPercentage += 0.5
priceOld -= priceOld * (reductionPercentage / 100)
priceNew -= priceNew * (reductionPercentage / 100)
}
return [monthsCount, Math.round(monthlySavings + priceOld - priceNew)]
}
console.log(nbMonths(2000, 8000, 1000, 1.5), [6, 766])
console.log(nbMonths(12000, 8000, 1000, 1.5), [0, 4000])
console.log(nbMonths(2300, 2800, 1000, 1.3), [1, 507])