How To Add To A Javascript Calculation At Specific Index
I have some fully working Javascript that is doing some calculations and displaying them into a table - its a 12 row table that represents a 12 month Forecast I added the fields
Solution 1:
I managed to do it this way :
functionforecastClient() {
const bankHolidays = newSet([
Date.parse("April, 19 2019"),
Date.parse("April, 22 2019"),
Date.parse("May, 6 2019"),
Date.parse("May, 27 2019"),
Date.parse("August, 26 2019"),
Date.parse("December, 25 2019"),
Date.parse("December, 26 2019"),
Date.parse("January, 1 2020"),
]);
functionaddWorkingDays(date, days) {
functionworkingDay(date) {
while (true) {
let day = (date.getDay() + 1) % 7;
if (day < 2) date.setDate(date.getDate() + 2 - day);
if (!bankHolidays.has(date.getTime())) break;
date.setDate(date.getDate() + 1);
}
}
workingDay(date);
while (days--) {
date.setDate(date.getDate() + 1);
workingDay(date);
}
return date;
}
const dateStr = document.querySelector("#startdate").value.replace(/-/g, "/");
const date = newDate(dateStr);
addWorkingDays(date, 0); // Make sure it is a working dayconst td = document.querySelectorAll("#forecast td.date-forecast");
for (let i = 0; i < 12; i++) {
td[i * 2].textContent = date.toDateString();
td[i * 2 + 1].textContent = addWorkingDays(date, 27).toDateString();
addWorkingDays(date, 1);
}
};
functionmyfunction() {
event.preventDefault();
doForecast(0);
}
functiondoForecast(index) {
var nextIndex = index + 1;
var startBalance = null;
var additionalAmount = (document.getElementById("additionalAmount").value != '') ? parseFloat(document.getElementById("additionalAmount").value) : null;
var additionalMonth = parseFloat(document.getElementById("additionalMonth").value);
var additionalAmount2 = (document.getElementById("additionalAmount2").value != '') ? parseFloat(document.getElementById("additionalAmount2").value) : null;
var additionalMonth2 = parseFloat(document.getElementById("additionalMonth2").value);
var additionalAmount3 = (document.getElementById("additionalAmount3").value != '') ? parseFloat(document.getElementById("additionalAmount3").value) : null;
var additionalMonth3 = parseFloat(document.getElementById("additionalMonth3").value);
if (index == 0) {
startBalance = parseFloat(document.getElementById("startBalance" + index).value);
if( additionalMonth == 1 && additionalAmount != undefined){
startBalance += additionalAmount;
}
if( additionalMonth2 == 1 && additionalAmount2 != undefined){
startBalance += additionalAmount2;
}
if( additionalMonth3 == 1 && additionalAmount3 != undefined){
startBalance += additionalAmount3;
}
} else {
startBalance = parseFloat(document.getElementById("amount" + index).innerHTML);
if( additionalMonth > 1 && additionalMonth == (index + 1) && additionalAmount != undefined){
startBalance += additionalAmount;
}
if( additionalMonth2 > 1 && additionalMonth2 == (index + 1) && additionalAmount2 != undefined){
startBalance += additionalAmount2;
}
if( additionalMonth3 > 1 && additionalMonth3 == (index + 1) && additionalAmount3 != undefined){
startBalance += additionalAmount3;
}
}
var interestRate = parseFloat(document.getElementById("interestRate").value);
var fee = parseFloat(document.getElementById("fee").value);
parseFloat(document.getElementById("interestRate").value);
var vat = parseFloat(document.getElementById("vat").value);
interestRate = interestRate / 100;
fee = fee / 100;
vat = vat / 100;
var simpleInt = startBalance * interestRate;
var profitfee = simpleInt * fee;
var afterVAT = profitfee * vat;
var amount = (startBalance + simpleInt - profitfee - afterVAT).toFixed(2)
var stringIndex = "" + (nextIndex);
document.getElementById("startBalance" + stringIndex).innerHTML = startBalance.toFixed(2);
document.getElementById("simpleInt" + stringIndex).innerHTML = simpleInt.toFixed(2);
document.getElementById("profitfee" + stringIndex).innerHTML = profitfee.toFixed(2);
document.getElementById("afterVAT" + stringIndex).innerHTML = afterVAT.toFixed(2);
document.getElementById("amount" + stringIndex).innerHTML = amount;
if (nextIndex < 12) {
doForecast(nextIndex);
}
}
functionstart() {
forecastClient();
myfunction();
}
<bodyonload="start();"><divclass="form-group col-mb-3"><labelonkeyup=""for="forecastLive">Live Date: <inputvalue="03/27/2019"id="startdate"></label><labelfor="startBalance">Start Balance
<inputid="startBalance0"value="1000"></label><inputtype="hidden"onkeyup="myfunction()"id="interestRate"value="20" /><inputtype="hidden"onkeyup="myfunction()"id="fee"value="30" /><inputtype="hidden"onkeyup="myfunction()"id="vat"value="20" /></div><br><divclass="form-group col-mb-3"><labelfor="additionalAmount">Aditional Amount
<inputonkeyup="myfunction()"id="additionalAmount"value=""></label><labelfor="Month">Month
<inputonkeyup="myfunction()"id="additionalMonth"value=""></label><br /><labelfor="additionalAmount2">Aditional Amount
<inputonkeyup="myfunction()"id="additionalAmount2"value=""></label><labelfor="Month">Month
<inputonkeyup="myfunction()"id="additionalMonth2"value=""></label><br /><labelfor="additionalAmount3">Aditional Amount
<inputonkeyup="myfunction()"id="additionalAmount3"value=""></label><labelfor="Month">Month
<inputonkeyup="myfunction()"id="additionalMonth3"value=""></label><br><br><style>.forecasttable,
.forecasttr,
.forecasttd,
.forecastth {
border: 1px solid;
border-collapse: collapse;
}
</style><tableclass="forecast table table-striped"id="forecast"onload="myfunction()"><tr><thscope="col">Month</th><thscope="col">Month Start</th><thscope="col">Investment</th><thscope="col">Return</th><thscope="col">Fee</th><thscope="col">Vat</th><thscope="col">Closing Balance</th><thscope="col">Month End</th></tr><tr><td>1</td><tdclass="date-forecast"></td><td><span>£</span><spanid="startBalance1"></span></td><td><span>£</span><spanid="simpleInt1"></span></td><td><span>£</span><spanid="profitfee1"></span></td><td><span>£</span><spanid="afterVAT1"></span></td><td><span>£</span><spanid="amount1"></span></td><tdclass="date-forecast"></td></tr><tr><td>2</td><tdclass="date-forecast"></td><td><span>£</span><spanid="startBalance2"></span></td><td><span>£</span><spanid="simpleInt2"></span></td><td><span>£</span><spanid="profitfee2"></span></td><td><span>£</span><spanid="afterVAT2"></span></td><td><span>£</span><spanid="amount2"></span></td><tdclass="date-forecast"></td></tr><tr><td>3</td><tdclass="date-forecast"></td><td><span>£</span><spanid="startBalance3"></span></td><td><span>£</span><spanid="simpleInt3"></span></td><td><span>£</span><spanid="profitfee3"></span></td><td><span>£</span><spanid="afterVAT3"></span></td><td><span>£</span><spanid="amount3"></span></td><tdclass="date-forecast"></td></tr><tr><td>4</td><tdclass="date-forecast"></td><td><span>£</span><spanid="startBalance4"></span></td><td><span>£</span><spanid="simpleInt4"></span></td><td><span>£</span><spanid="profitfee4"></span></td><td><span>£</span><spanid="afterVAT4"></span></td><td><span>£</span><spanid="amount4"></span></td><tdclass="date-forecast"></td></tr><tr><td>5</td><tdclass="date-forecast"></td><td><span>£</span><spanid="startBalance5"></span></td><td><span>£</span><spanid="simpleInt5"></span></td><td><span>£</span><spanid="profitfee5"></span></td><td><span>£</span><spanid="afterVAT5"></span></td><td><span>£</span><spanid="amount5"></span></td><tdclass="date-forecast"></td></tr><tr><td>6</td><tdclass="date-forecast"></td><td><span>£</span><spanid="startBalance6"></span></td><td><span>£</span><spanid="simpleInt6"></span></td><td><span>£</span><spanid="profitfee6"></span></td><td><span>£</span><spanid="afterVAT6"></span></td><td><span>£</span><spanid="amount6"></span></td><tdclass="date-forecast"></td></tr><tr><td>7</td><tdclass="date-forecast"></td><td><span>£</span><spanid="startBalance7"></span></td><td><span>£</span><spanid="simpleInt7"></span></td><td><span>£</span><spanid="profitfee7"></span></td><td><span>£</span><spanid="afterVAT7"></span></td><td><span>£</span><spanid="amount7"></span></td><tdclass="date-forecast"></td></tr><tr><td>8</td><tdclass="date-forecast"></td><td><span>£</span><spanid="startBalance8"></span></td><td><span>£</span><spanid="simpleInt8"></span></td><td><span>£</span><spanid="profitfee8"></span></td><td><span>£</span><spanid="afterVAT8"></span></td><td><span>£</span><spanid="amount8"></span></td><tdclass="date-forecast"></td></tr><tr><td>9</td><tdclass="date-forecast"></td><td><span>£</span><spanid="startBalance9"></span></td><td><span>£</span><spanid="simpleInt9"></span></td><td><span>£</span><spanid="profitfee9"></span></td><td><span>£</span><spanid="afterVAT9"></span></td><td><span>£</span><spanid="amount9"></span></td><tdclass="date-forecast"></td></tr><tr><td>10</td><tdclass="date-forecast"></td><td><span>£</span><spanid="startBalance10"></span></td><td><span>£</span><spanid="simpleInt10"></span></td><td><span>£</span><spanid="profitfee10"></span></td><td><span>£</span><spanid="afterVAT10"></span></td><td><span>£</span><spanid="amount10"></span></td><tdclass="date-forecast"></td></tr><tr><td>11</td><tdclass="date-forecast"></td><td><span>£</span><spanid="startBalance11"></span></td><td><span>£</span><spanid="simpleInt11"></span></td><td><span>£</span><spanid="profitfee11"></span></td><td><span>£</span><spanid="afterVAT11"></span></td><td><span>£</span><spanid="amount11"></span></td><tdclass="date-forecast"></td></tr><tr><td>12</td><tdclass="date-forecast"></td><td><span>£</span><spanid="startBalance12"></span></td><td><span>£</span><spanid="simpleInt12"></span></td><td><span>£</span><spanid="profitfee12"></span></td><td><span>£</span><spanid="afterVAT12"></span></td><td><span>£</span><spanid="amount12"></span></td><tdclass="date-forecast"></td></tr></table>
Code also available here : https://codepen.io/tmacpolo/pen/WmaRVr
EDIT: Update code to allow 3 amount possibilities
Post a Comment for "How To Add To A Javascript Calculation At Specific Index"