karina karina - 1 month ago 11
Javascript Question

calculate straight line declining

i have asset (say vehicle) with value of 1000 in this year...

var assetCurrentBookValue = 1000


vehicle will depreciate within 5 years... that means the value will become 0 in the next 5 years...

var assetLife = 5


yearly depreciation become...

var assetYearlyDepreciation = assetCurrentBookValue / assetLife


i need to calculate yearly value after yearly depreciation like so...

year assetYearlyDepreciation assetBookVaue
2016 200 1000
2017 200 800
2018 200 600
2019 200 400
2020 200 200
2021 200 0


i need to convert into array like so...

[
[2016,1000],
[2017,800],
[2018,600],
[2019,400],
[2020,200],
[2021,0]
]


here my workaround...

var index = Array.from(Array(assetLife).keys());
var thisYear = new Date().getFullYear()

var assetYearlyBookValue = [[thisYear,assetCurrentBookValue]]

index.forEach((o) => {
assetYearlyBookValue.push([o + thisYear + 1, assetCurrentBookValue - assetYearlyDepreciation])
});


but ive got..

[
[2016,1000],
[2017,800],
[2018,800],
[2019,800],
[2020,800],
[2021,800]
]


any help would be appreciated... thank Youu..

Answer

Modify your results from assetCurrentBookValue - assetYearlyDepreciation and new Date().getFullYear() and store them into variables so that you can get a continuously changing result:

var index = Array.from(Array(assetEconomicLife).keys());
var thisYear = new Date().getFullYear()

var assetYearlyBookValue = [[thisYear,assetCurrentBookValue]]
var lastRes = assetCurrentBookValue

index.forEach((o) => {
    lastRes -= assetYearlyDepreciation
    assetYearlyBookValue.push([o + (++thisYear), lastRes])
});