fasteddie
fasteddie

Reputation: 17

Sum array of the increment of input values

I'm trying to write a sum array function that takes two input values. The first input value is added incrementally until it reaches the value of the second input. For example if the lowerLimit is '2' and the upperLimit is '5' the total sum would be 2 + (2+1) + (2+2) + (2+3) + (2+4) + (2+5) = 27. I've tried using the variable 'i' that is added to the lowerLimit and goes up incrementally until (i <= (upperLimit - lowerLimit) but I'm unsure of how to do write the sum array, or if a sum array is even the best way to approach this. Thanks for your help.

Q7 Display

Q7

<!-- Question 7 Start -->
<div role="tabpanel" class="tab-pane tab-pane active" id="q6">
    <div class="row">
        <div class="col-md-12">
            <pre>     

Question 7 code:

  </pre>
        </div>
        <div class="col-md-12">
            <!-- button -->
            <button id="button" class="btn btn-default" type="button">Question Seven Solution</button>
        </div>
        <div class="col-md-12">
            <!-- result -->
            <div id="result"></div>
        </div>
    </div>
</div>




    <script>
        $(document).ready(function () {
            var lowerLimit = 0;
            var upperLimit = 0;

            function range(lowerLimit, upperLimit){
                var lowerLimit = parseInt(prompt("What number would you like to being with?"));
                if (isNaN(lowerLimit)) {
                    alert("That's not a number, please retry.");
                    var lowerLimit = prompt("Please re-enter a number.");
                }
                var upperLimit = parseInt(prompt("What number would you like to end with?"));
                if (isNaN(upperLimit)) {
                    alert("That's not a number, please retry.");
                    var upperLimit = prompt("Please re-enter a number.");
                }

             /*    var arr = [upperLimit, lowerLimit, i];
                for(i = 1; i <= (upperLimit - lowerLimit); i++){
                var equation = lowerLimit + (lowerLimit+i);

                }*/


            //ends function
            }
            //ends document ready function
        });
    </script>

Upvotes: 0

Views: 1497

Answers (2)

supraja
supraja

Reputation: 26

what is use of arrays? You can directly add them and give output without using arrays...

try this code:

var sum=lowerLimit; for(i=1; i<=upperLimit; i++){ sum += (lowerLimit+i); }

Upvotes: 1

supraja
supraja

Reputation: 26

sorry there is a mistake in that code

var sum=lowerlimit;
for(i=1; i<=upperLimit; i++){
  sum += (lowerLimit+i);
}

Upvotes: 0

Related Questions