1

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?

6
  • 4
    At this stage in your learning process, it's safe to just never even consider using document.write(). Ever. Commented Sep 10, 2013 at 14:17
  • 1
    You should use return according to me. Commented Sep 10, 2013 at 14:18
  • 1
    @Pointy I am also beginner to this language. Can you tell me,why not safe? I am asking out of curiosity. Commented Sep 10, 2013 at 14:21
  • So the code would read... return[i]? Commented Sep 10, 2013 at 14:23
  • 1
    @ChrisCarter document.write() is not really part of JavaScript. It's a browser operation that's really only useful in a few special cases. Commented Sep 10, 2013 at 15:03

2 Answers 2

4

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

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++?
++i will add 1 after the for loop instead of before, I guess. The difference in this case ... I don't know.
@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 .
@DipakIngole, callback is missing from this. amountTotal(amount);.
Please note that he used amount.length instead of 35. You should always try to avoid "magic numbers" if you can.
2
function sumArray(arr){
    var total = 0;
    arr.forEach(function(element){
        // 'element' in the parenthesis can be any name
        total += element;
    });
    return total;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.