I'm attempted to learn Javascript (again) due to a new role at work and am struggling a little. As a learning exercise I'm trying to code a simple set of functions to get used to the simple skills and have come across a problem. Code is below, but I have a webpage I've built to capture some data and the pass into some simple interest calculations. Unfortunately I'm getting a Nan error when the values hit the functions and I don't know how to resolve it.
I've tried +document, parse(float) and capturing the innerhtml from the fields, but none of those worked so I'm a little stumped. I'm certain it's a typecasting issue but no idea how to resolve it.
I'm capturing the fields used in the calculations via:
var runCalculations = new function() {
this.calculateTotalPayable = function() {
this.submitInitialValues();
this.simpleInterestCalc();
this.monthlyCompInterestCalc();
this.dailyCompInterestCalc();
}
this.submitInitialValues = function() {
//Initial loan or investment amount
var initialAmount = +document.getElementById("initialAmount").value;
console.log("Initial loan amount: " + initialAmount);
//Interest rate
var interestRate = +document.getElementById("interestRate").value;
console.log("Interest rate: " + interestRate);
//Tenure of loan
var loanTenure = +document.getElementById("loanTenure").value;
console.log("Loan tenure: " + loanTenure);
}
this.simpleInterestCalc = function() {
var simpleInterestTotal = 0;
console.log(initialAmount + interestRate + loanTenure);
simpleInterestTotal = initialAmount + (initialAmount * (interestRate / 100) * loanTenure);
console.log("simple " + simpleInterestTotal);
}
this.monthlyCompInterestCalc = function() {
const monthCompInterestTotal = 0;
console.log("month " + monthCompInterestTotal);
}
this.dailyCompInterestCalc = function() {
const dailyCompInterestTotal = 0;
console.log("daily " + dailyCompInterestTotal);
}
}
<form id="initialValues">
<label for="initialAmount" style="margin-left: 50px"><b>Initial loan or investment amount</b></label><br>
<input type="number" id="initialAmount" name="initialAmount" style="margin-left: 50px" value="0"><br>
<label for="interestRate" style="margin-left: 50px"><b>Interest rate</b></label><br>
<input type="number" id="interestRate" name="interestRate" style="margin-left: 50px" value="0"><br>
<label for="loanTenure" style="margin-left: 50px"><b>Loan tenure</b></label><br>
<input type="number" id="loanTenure" name="loanTenure" style="margin-left: 50px" value="0"><br>
<br>
<input type="button" style="margin-left: 50px" value="Submit" onclick="runCalculations.calculateTotalPayable()">
<input type="reset" style="margin-left: 50px" value="Reset Values">
</form>