Skip to content

Instantly share code, notes, and snippets.

@wolthers
Last active January 16, 2018 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolthers/022c49d64d7c145ff45ade5838328a81 to your computer and use it in GitHub Desktop.
Save wolthers/022c49d64d7c145ff45ade5838328a81 to your computer and use it in GitHub Desktop.
const cliTable = require('cli-table')
function calc(purchaseValue, entryYear, numYears) {
const table = new cliTable({
head: ['Periode', 'Bilens værdi', '%', 'Værditab i alt', '%', 'Årets værditab', 'Årets værditab pr. mnd', 'Ydelse pr. mnd over hele perioden'],
colWidths: [9, 14, 4, 16, 4, 16, 24, 35]
})
const percentualValueEachYear = [100, 81, 67, 56, 48, 39, 34, 29, 25, 22, 19, 18, 16].slice(entryYear)
// If entryYear is 0, 19, 14, 11, 8, 9, 5, 5, 4, 3, 3, 1, 2
const percentualLossEachYear = percentualValueEachYear.map((value, index, arr) => (arr[index - 1] || 100) - value)
const computedPercentageValues = percentualValueEachYear.map((value, index) => value + percentualLossEachYear[0])
for (let i = 0; i < numYears; i++) {
const valueLastYear = purchaseValue * (computedPercentageValues[i] / 100)
const valueThisYear = purchaseValue * (computedPercentageValues[i + 1] / 100)
const valueThisYearPct = computedPercentageValues[i + 1]
const accumulatedLoss = purchaseValue - valueThisYear
const yearlyLoss = valueLastYear - valueThisYear
const monthlyLoss = yearlyLoss / 12
const months = (i + 1) * 12
table.push([
months + ' mnd.',
formatNumber(Math.round(valueThisYear)),
valueThisYearPct,
formatNumber(Math.round(accumulatedLoss)),
100 - computedPercentageValues[i + 1],
formatNumber(Math.round(yearlyLoss)),
formatNumber(Math.round(monthlyLoss)),
formatNumber(Math.round(accumulatedLoss / months))
])
}
return table.toString()
}
function formatNumber(num) {
const x = String(num).split('.')
const x2 = x.length > 1 ? ',' + x[1] : ''
const rgx = /(\d+)(\d{3})/
let x1 = x[0]
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + '.' + '$2')
}
return x1 + x2
}
function logExample(purchaseValue, manufactureYear, thisYear = 2018) {
console.log('Årgang', manufactureYear, '- købspris', formatNumber(purchaseValue) + ',-')
console.log(calc(purchaseValue, thisYear - manufactureYear, 5))
console.log()
}
logExample(220000, 2018)
logExample(220000, 2017)
logExample(220000, 2016)
logExample(220000, 2015)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment