Code_Ed_Student
Code_Ed_Student

Reputation: 1190

Calculating investment values with javascript

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 future value. 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 0 or greater than 20 but nothing is showing. How would i be able to properly display the correct future value and alerts when necessary? Example

Javascript

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 <= 0) {
        alert("Investment must be a valid number\nand greater than zero.");
    } else if(isNaN(annualRate) || annualRate <= 0 || annualRate > 20) {
        alert("Annual rate must be a valid number\nand less than or equal to 20.");
    } else if(isNaN(years) || years <= 0 || years > 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 <= 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;
}

Upvotes: 0

Views: 2645

Answers (3)

Yashar Baradarvar
Yashar Baradarvar

Reputation: 11

// to calculate the increase in production for each month

 function getPlan(startProduction, numberOfMonths, percent) { // the function is declared

  let Goals = [];   //Goals is assigned

  let currentProduction=startProduction;   //declaring the currentProduction by assigning the value of startProduction

  for(let i=0; i<numberOfMonths;i++){   //for each month, increase the currentProduction by the percentage       interest = Math.floor(currentProduction+(currentProduction*(percent/100)));

     currentProduction= interest;
    //after each iteration, assign the new currentProduction

     Goals.push(Math.floor(currentProduction));
    //adding the currentprodcution of each months to the Goals

 }  return Goals; } console.log(getPlan(2000,6,67));

Output: [ 3340, 5577, 9313, 15552, 25971, 43371 ]

Upvotes: 0

Niall Paterson
Niall Paterson

Reputation: 3580

Using .value is incorrect, its javascript, while this is jquery, try adding a # in front and use .val() instead.

Its similar to this:

jquery function val() is not equivalent to "$(this).value="?

EDIT

He's not using jquery, ignore this.

Upvotes: 1

Jon
Jon

Reputation: 4736

If I remember the future value correctly, you are messing up the formula, which is why you aren't getting the expected value.

Change:

    for ( i = 1; i <= years; i++ ) {
        futureValue = ( futureValue + investment ) *
            ( 1 + annualRate );
    }

To:

futureValue = investment*Math.pow((1+annualRate), years);

Not quite sure why you are looping through each year, but it should be based on powers to the number of years (again, if I remember correctly)

Upvotes: 1

Related Questions