Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculating investment values with javascript
    primarykey
    data
    text
    <p>I am currently learning javascript. I have created a calculator to find invesment future value. It is giving me an incorrect value when it displays the <code>future value</code>. I have checked the formula several times but it still gives me an error. Also, I have set alerts to appear if the interest is less than <code>0</code> or greater than <code>20</code> but nothing is showing. How would i be able to properly display the correct future value and alerts when necessary? <a href="http://webprolearner2346.zxq.net/calculator/future_value.html" rel="nofollow">Example</a></p> <p><strong>Javascript</strong></p> <pre><code>var $ = function (id) { return document.getElementById(id); } var calculate_click = function () { var investment = parseFloat( $("investment").value ); var annualRate = parseFloat( $("rate").value ) /100; var years = parseInt( $("years").value ); $("futureValue").value = ""; if (isNaN(investment) || investment &lt;= 0) { alert("Investment must be a valid number\nand greater than zero."); } else if(isNaN(annualRate) || annualRate &lt;= 0 || annualRate &gt; 20) { alert("Annual rate must be a valid number\nand less than or equal to 20."); } else if(isNaN(years) || years &lt;= 0 || years &gt; 50) { alert("Years must be a valid number\nand less than or equal to 50."); } else { //var monthlyRate = annualRate / 12; //var months = years * 12; var futureValue = 0; for ( i = 1; i &lt;= years; i++ ) { futureValue = ( futureValue + investment ) * ( 1 + annualRate ); } $("futureValue").value = futureValue.toFixed(2); } } var clear_click = function () { $("investment").value = ""; $("rate").value = ""; $("years").value = ""; $("futureValue").value = ""; } window.onload = function () { $("calculate").onclick = calculate_click; $("investment").focus(); $("clear").onclick = clear_click; } </code></pre>
    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.
 

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