0

I am working on a specific calculator and I need to sum up variables in my jQuery.each for a range from 0 to 4 (= 5 years).

I have found many pages where the solution is described, but only with references to an element, not to the range.

My code:

    jQuery.each(new Array(duration),
        function(n){

            var investment = 1000;    // 1000$
            var duration = 5;         // 5 years
            var revenueRatio = 10;    // 10% revenue / year
            var reinvest = 50;        // 50% reinvestment

            if(n == 0){

                var revenueReinvest = 0;
                var newInvestment = investment;

            }else{

                console.log(revenueReinvest);    // undefined
                console.log(newInvestment);      // undefined

                var interest = ( ( newInvestment - investment ) * ( revenueRatio / 100 ) );
                var removeInterest = interest * reinvest / 100;
                var restIntereset = interest - removeInterest;

                revenueReinvest += restIntereset;
                newInvestment = newInvestment + revenueReinvest;

            }
        }
    );

Any help or idea would be great! Thank you!

2
  • What is your actual issue? Commented Aug 3, 2021 at 9:49
  • If I do console.log(revenueReinvest); I get undefined. I want to sum up to this variable, because I need it later on. Commented Aug 3, 2021 at 9:50

1 Answer 1

1

The issue with the code is that you have declared revenueReinvest and newInvestment inside the if block and using it inside the else block. This wont be possible. Declare revenueReinvest and newInvestment outside each loop and assign the values to them inside if statement. Now you can access the assigned values inside else statement.

You have to declare the variable outside the loop to prevent redeclaring of the variable inside the loop. Each time the variable gets redeclared inside the loop, old value will be lost.

The below code will work

$(document).ready(function () {
  const duration = 4;
  // Declare here
  var revenueReinvest;
  var newInvestment;
  jQuery.each(new Array(duration),
    function (n) {
      var investment = 1000;    // 1000$
      var duration = 5;         // 5 years
      var revenueRatio = 10;    // 10% revenue / year
      var reinvest = 50;        // 50% reinvestment

      if (n == 0) {
        // assign value here
        revenueReinvest = 0;
        newInvestment = investment;
      } else {
        var interest = ((newInvestment - investment) * (revenueRatio / 100));
        var removeInterest = interest * reinvest / 100;
        var restIntereset = interest - removeInterest;

        revenueReinvest += restIntereset;
        newInvestment = newInvestment + revenueReinvest;
      }
    }
  );
  console.log(revenueReinvest);    // will be defined
  console.log(newInvestment);      // will be defined
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

3 Comments

First of all: Thank you! But the problem is that this variables are set back in every loop. I need to add up to this variable in every loop to use it later on (after the each loop).
@AliElkhaiat can you check my updated answer? You have to declare the variable outside the loop to prevent reassigning of the variable inside the loop.
Works perfectly, thank you! The variables should be outside the each loop!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.