0

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>
2
  • Tagged Python. Did you mean javascript? Commented Feb 24, 2016 at 2:44
  • @Evert yes, I meant this to be a javascript question Commented Feb 24, 2016 at 18:45

2 Answers 2

1

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); }

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

3 Comments

thanks for the help, that does make it a lot easier. I've added this since i starts at a value of 'for(i=1; i<= (upperLimit + 1); i++){ sum = lowerLimit + (lowerLimit+i); }' So when lowerLimit = 1 and upperLimit = 3 the sum (1+2+3) = 6. However when lowerLimit = 1 and upperLimit = 4 the sum = 7 even though it should equal 10 (1+2+3+4).
sorry there was a small mistake in code... it should be 'sum += lowerLimit + (lowerLimit+i);'
Thanks a lot for your help. I edited the parameter on i to i<=(upperLimit - lowerLimit) and the function works as needed. I appreciate the help.
0

sorry there is a mistake in that code

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

1 Comment

@xpy Thanks a lot guys for your help. I edited the parameter on i to i<=(upperLimit - lowerLimit) and the function works as needed. Thanks again.

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.