0

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;
}
Code_Ed_Student
  • 1,180
  • 6
  • 27
  • 67
  • Can you please give example inputs and expected output along with the output you are getting? – Jon Apr 04 '13 at 21:15
  • On a side note, I think you aren't checking the correct values for `annualRate`, as the first thing you do is divide by `100`, so you should be checking for `> .2` – Jon Apr 04 '13 at 21:22

3 Answers3

1

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.

Community
  • 1
  • 1
Niall Paterson
  • 3,580
  • 3
  • 29
  • 37
  • But this isn't JQuery, check the first function assignment: `var $ = function (id) { return document.getElementById(id); }` – Jon Apr 04 '13 at 20:56
  • is there any reason you did it this way? That function isn't working anyway see - http://jsfiddle.net/avXkd/ why not use jquery, add a hash in front of 'investment' etc and use .val()? I don't mind if this is an assignment, so were you told not to? – Niall Paterson Apr 04 '13 at 20:59
  • @Niall that because you can't get a div value that way in pure JS - and I also believe JSFiddle prevents the assignment of the `$` function within the JS. On a side note, Code_Ed_Student - JQuery is Javascript, so saying that it is javascript can be inclusive to JQuery, whereas saying it isn't JQuery doesn't exclude it from being JS ;) – Jon Apr 04 '13 at 21:04
  • @Jon but 'investment' at least isn't a div its an input (see $("investment").focus();) Maybe it isn't the part I initially taught - http://jsfiddle.net/avXkd/2/ this works fine. – Niall Paterson Apr 04 '13 at 21:08
  • Ok, so it doesn't prevent the assignment of the `$` function - good to know, but the `.value` does appear to be working fine. ^^ – Jon Apr 04 '13 at 21:14
  • very true, @Code_Ed_Student maybe if you pop a few console.log's into your script it might help? just to get a better idea of whats working and whats not? – Niall Paterson Apr 04 '13 at 21:22
  • @Niall I believe the problem was the formula as I got no errors running his code, Though the question was misleading as he did say 'error' for the future value. ^^ – Jon Apr 04 '13 at 21:34
  • I agree with your answer, that was my initial thought, but I'm more perplexed with `Also, I have set alerts to appear if the interest is less than 0` You're right about the 20, but why would the check for 0 not be firing? All a little odd. – Niall Paterson Apr 04 '13 at 21:37
  • The alerts do fire, just checked out his page again - if you put 0 or a negative in those boxes the alert does fire. – Jon Apr 04 '13 at 21:38
  • Oh grand I didn't see that, must be the formula then yes. – Niall Paterson Apr 04 '13 at 21:39
1

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)

Jon
  • 4,746
  • 2
  • 24
  • 37
0
// 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 ]

Zach Jensz
  • 3,650
  • 5
  • 15
  • 30