I’m new to JavaScript and trying to learn more about what it can do and how to use it.
is it possible to return multiple results as a result of a calculation?
I am working on a calculator for a class project. What I would like it to do is return 3 values on my page:
Interest rate
total amount borrowed and
monthly repayment
So far I have managed to get it to display monthly repayment in a div on the page, but I would like to be able to show all 3 on the page as a result of the one calculation.
Is this possible?
Here is what I have come up with so far:
HTML:
<p><input type="button" onclick="main();" value="Calculate"></p>
JavaScript:
function main()
{
var userInput1 = 0;
var userInput2 = 0;
var displayResult;
userInput1 = document.getElementById("loan_amount").value;
userInput1 = parseFloat(userInput1);
userInput2 = document.getElementById("loan_term").value;
userInput2 = parseFloat(userInput2);
displayResult = calcLoan(userInput1,userInput2);
document.getElementById("Result1").innerHTML=displayResult;
}
function calcLoan(userInput1,userInput2)
{
var interest =0;
if (userInput1 <1000)
{
alert("Please enter a value above £1000")
}
else if (userInput1 <= 10000)
{
interest = 4.25 + 5.5;
}
else if (userInput1 <= 50000)
{
interest = 4.25 + 4.5;
}
else if (userInput1 <= 100000)
{
interest = 4.25 + 3.5;
}
else
{
interest = 4.25 + 2.5;
}
var totalLoan = 0;
totalLoan = userInput1 +(userInput1*(interest/100))*userInput2;
var monthlyRepayment = 0;
var monthly;
monthlyRepayment = totalLoan/(userInput2*12);
monthly=monthlyRepayment.toFixed(2);
alert("Interest Rate = " + interest + "%" +" "+"Total Loan amount = " + "£"+ totalLoan +" "+ "Your monthly repayment is = " + " " + "£"+ monthly);
return monthly;
}
If anyone can point me in the right direction, it would be great!
You can create variables with multiple custom fields and pass them between functions. So, your functions should look like this:
And don’t forget to add div elements with IDs Result1, Result2 and Result3.