How do I return the sum of all values in an array?

14,315

Solution 1

Return a value from function.

function amountTotal(amount) {
        var total = 0;
        for (i = 0; i < amount.length; ++i) {
             total += amount[i]; // add each element in an array to total
        }
        return total;// return sum of elements in array
}

Solution 2

function sumArray(arr){
    var total = 0;
    arr.forEach(function(element){
        // 'element' in the parenthesis can be any name
        total += element;
    });
    return total;
}
Share:
14,315
Chris Carter
Author by

Chris Carter

Updated on June 04, 2022

Comments

  • Chris Carter
    Chris Carter almost 2 years

    I am very new to javascript and am having a problem figuring out if I should use return or document.write or both to return the sum of all values in an array called 'amount'. I am supposed to create a function names amountTotal().

    The purpose of which is to return the sum of all values in the amount array. Then I am to declare a variable named total, setting its initial value to 0. Then create a for loop that loops through all the values in the amount array.

    At each iteration of the loop, add the current value of the array item to the value of the total variable. Finally, after the loop is completed I need to return the value of the total variable. The largest array value is [34]. This value will be written to a table called Summary

    This is what I have written so far.

    <script type="text/javascript">
        function amountTotal() {
            var total = 0;
            for (i = 0; i < 35; i++) {
                document.write("<td>" + i + "</td>")
            }
        }
    </script>
    

    Am I on the right track?

    • Pointy
      Pointy over 10 years
      At this stage in your learning process, it's safe to just never even consider using document.write(). Ever.
    • Arpit
      Arpit over 10 years
      You should use return according to me.
    • Arup Rakshit
      Arup Rakshit over 10 years
      @Pointy I am also beginner to this language. Can you tell me,why not safe? I am asking out of curiosity.
    • Chris Carter
      Chris Carter over 10 years
      So the code would read... return[i]?
    • Arpit
      Arpit over 10 years
      if you use return, then call the function from script not from HTML element.
    • Pointy
      Pointy over 10 years
      @ChrisCarter document.write() is not really part of JavaScript. It's a browser operation that's really only useful in a few special cases.
  • Chris Carter
    Chris Carter over 10 years
    Awesome! Thanks captain.
  • Chris Carter
    Chris Carter over 10 years
    Will do. Thanks. Also, a comment about your suggestion. I failed to indicate that the amountTotal() was to have no parameters. Will this make a difference? And why use ++i instead of i++?
  • Momro
    Momro over 10 years
    ++i will add 1 after the for loop instead of before, I guess. The difference in this case ... I don't know.
  • Deepak Ingole
    Deepak Ingole over 10 years
    @ChrisCarter ++i is always more preferred over i++. i++ reads the value of i then increments it. ++i increments the value of i then reads it.In for loop it actually doesnt makes a difference.It just a way of coding.But this will surely make a difference if you return ++i or i++.Try returning ++i and i++ .Nice question.If you are not passing parameter to function you should declare the variable globally or locally so that yo can access it .
  • Daniel
    Daniel about 6 years
    @DipakIngole, callback is missing from this. amountTotal(amount);.
  • FalcoGer
    FalcoGer almost 4 years
    Please note that he used amount.length instead of 35. You should always try to avoid "magic numbers" if you can.