Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning multiple results in innerHTML
    primarykey
    data
    text
    <p>I'm new to JavaScript and trying to learn more about what it can do and how to use it.</p> <p>is it possible to return multiple results as a result of a calculation?</p> <p>I am working on a calculator for a class project. What I would like it to do is return 3 values on my page:</p> <p><code>Interest rate</code></p> <p><code>total amount borrowed</code> and <code>monthly repayment</code></p> <p>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.</p> <p>Is this possible?</p> <p>Here is what I have come up with so far: HTML: <code>&lt;p&gt;&lt;input type="button" onclick="main();" value="Calculate"&gt;&lt;/p&gt;</code></p> <p>JavaScript:</p> <pre><code>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 &lt;1000) { alert("Please enter a value above £1000") } else if (userInput1 &lt;= 10000) { interest = 4.25 + 5.5; } else if (userInput1 &lt;= 50000) { interest = 4.25 + 4.5; } else if (userInput1 &lt;= 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; } </code></pre> <p>If anyone can point me in the right direction, it would be great!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload