Skip to content

Instantly share code, notes, and snippets.

@petehouston
Created March 18, 2020 15:00
Show Gist options
  • Save petehouston/95a2eba19b5aeb252142728c80054371 to your computer and use it in GitHub Desktop.
Save petehouston/95a2eba19b5aeb252142728c80054371 to your computer and use it in GitHub Desktop.
Calculate
function calculateAverageBaseRate(
leaseTerm,
baseRate,
escalationValue,
escalationType,
freeRentType,
opex,
sizeSf,
numOfFreeMonths,
) {
// variables
let averageBaseRate = 0
// early return due to free duration
if (leaseTerm <= numOfFreeMonths) {
return 0;
}
// helper utils
let round2digits = (num) => (Math.round(num * 100)) / 100;
let accumulateBaseRate = escalationValue => rate => rate + rate * escalationValue / 100;
// in between a year
if (numOfFreeMonths < leaseTerm && leaseTerm <= numOfFreeMonths + 11) {
return round2digits((baseRate * (leaseTerm - numOfFreeMonths)) / leaseTerm);
}
// accumulator for base rate during yearly interval
let accumulatorBaseRate = accumulateBaseRate(escalationValue);
// over a year accumulation
// TODO: numberOfFreeMonths bigger than 12
let totalBaseRent = 0
let totalBaseRentFirstYear = 0
let monthsToCalculate = 0
let shouldStartPeriod = false
if (numOfFreeMonths <= 12) {
totalBaseRentFirstYear = (baseRate / 12) * sizeSf * (12 - numOfFreeMonths);
monthsToCalculate = leaseTerm - 12;
shouldStartPeriod = true;
// console.log('totalBaseRentFirstYear = ' + totalBaseRentFirstYear)
} else {
totalBaseRentFirstYear = 0;
monthsToCalculate = leaseTerm - numOfFreeMonths;
shouldStartPeriod = false;
}
// console.log('monthsToCalculate = ' + monthsToCalculate)
totalBaseRent = totalBaseRentFirstYear; // initial value for first fiscal year
let accBaseRate = 0 // accumulator for base rate
// next calculation is less than a year
if (monthsToCalculate <= 12) {
// this time, base rate is accumulated
accBaseRate = !shouldStartPeriod ? baseRate : baseRate + baseRate * escalationValue / 100;
// console.log(accBaseRate)
totalBaseRent += ( (accBaseRate/12) * sizeSf * monthsToCalculate );
}
// make interval calculation
else {
let intervalYear = parseInt(monthsToCalculate / 12);
let remainMonths = monthsToCalculate - (12 * intervalYear);
// console.log('intervalYear = ' + intervalYear)
// console.log('remainMonths = ' + remainMonths)
// console.log('shouldStartPeriod = ' + (shouldStartPeriod))
// initial accumulate value of base rate
accBaseRate = baseRate
for (let i = 0; i < Math.round(intervalYear); i++) {
if (!shouldStartPeriod) {
shouldStartPeriod = true
} else {
accBaseRate = (accumulatorBaseRate(accBaseRate))
}
totalBaseRent += (accBaseRate * sizeSf);
// console.log('accBaseRate = ' + accBaseRate + ' | totalRent = ' + (accBaseRate * sizeSf))
}
if (remainMonths > 0) {
accBaseRate = (accumulatorBaseRate(accBaseRate))
totalBaseRent += ((accBaseRate / 12) * sizeSf * remainMonths);
// console.log('accBaseRate = ' + accBaseRate + ' | totalRent = ' + ((accBaseRate / 12) * sizeSf * remainMonths))
}
}
// console.log('totalBaseRent = ' + totalBaseRent)
// calculate average base rate
averageBaseRate = ((totalBaseRent / leaseTerm) / sizeSf) * 12;
// round up to 2 decimal digits
// return parseFloat(averageBaseRate.toFixed(2));
return round2digits(averageBaseRate)
}
////////////////////////////////
// TESTING
////////////////////////////////
function test(expect, actual) {
if (expect === actual) {
console.log('[OKAY] ' + expect + ' = ' + actual)
} else {
console.log('DAMN IT -_-; ---> ' + expect + ' != ' + actual)
}
}
/// free months = 4
test(0, calculateAverageBaseRate(4, 13.50, 3, 'Percent', 'Base', 4.32, 25245, 4))
test(2.7, calculateAverageBaseRate(5, 13.50, 3, 'Percent', 'Base', 4.32, 25245, 4))
test(9, calculateAverageBaseRate(12, 13.50, 3, 'Percent', 'Base', 4.32, 25245, 4))
test(9.9, calculateAverageBaseRate(15, 13.50, 3, 'Percent', 'Base', 4.32, 25245, 4))
test(10.23, calculateAverageBaseRate(16, 13.50, 3, 'Percent', 'Base', 4.32, 25245, 4))
test(11.45, calculateAverageBaseRate(24, 13.50, 3, 'Percent', 'Base', 4.32, 25245, 4))
test(11.57, calculateAverageBaseRate(25, 13.50, 3, 'Percent', 'Base', 4.32, 25245, 4))
test(11.67, calculateAverageBaseRate(26, 13.50, 3, 'Percent', 'Base', 4.32, 25245, 4))
test(13.27, calculateAverageBaseRate(55, 13.50, 3, 'Percent', 'Base', 4.32, 25245, 4))
/// free months = 12
test(1.93, calculateAverageBaseRate(14, 13.50, 3, 'Percent', 'Base', 4.32, 25245, 12))
/// free months = 13
test(0.96, calculateAverageBaseRate(14, 13.50, 3, 'Percent', 'Base', 4.32, 25245, 13))
test(1.80, calculateAverageBaseRate(15, 13.50, 3, 'Percent', 'Base', 4.32, 25245, 13))
test(6.19, calculateAverageBaseRate(24, 13.50, 3, 'Percent', 'Base', 4.32, 25245, 13))
test(6.48, calculateAverageBaseRate(25, 13.50, 3, 'Percent', 'Base', 4.32, 25245, 13))
test(8.10, calculateAverageBaseRate(32, 13.50, 3, 'Percent', 'Base', 4.32, 25245, 13))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment