How do I return the sum of all values in an array?
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;
}
Chris Carter
Updated on June 04, 2022Comments
-
Chris Carter almost 2 years
I am very new to javascript and am having a problem figuring out if I should use
return
ordocument.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 namedtotal
, setting its initial value to 0. Then create a for loop that loops through all the values in theamount
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 thetotal
variable. The largest array value is[34]
. This value will be written to a table calledSummary
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 over 10 yearsAt this stage in your learning process, it's safe to just never even consider using
document.write()
. Ever. -
Arpit over 10 yearsYou should use
return
according to me. -
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 over 10 yearsSo the code would read... return[i]?
-
Arpit over 10 yearsif you use return, then call the function from script not from HTML element.
-
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 over 10 yearsAwesome! Thanks captain.
-
Chris Carter over 10 yearsWill 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 over 10 years
++i
will add 1 after thefor
loop instead of before, I guess. The difference in this case ... I don't know. -
Deepak Ingole over 10 years@ChrisCarter
++i
is always more preferred overi++
.i++
reads the value ofi
then increments it.++i
increments the value ofi
then reads it.Infor
loop it actually doesnt makes a difference.It just a way of coding.But this will surely make a difference if you return++i
ori++
.Try returning++i
andi++
.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 about 6 years@DipakIngole, callback is missing from this.
amountTotal(amount);
. -
FalcoGer almost 4 yearsPlease note that he used
amount.length
instead of 35. You should always try to avoid "magic numbers" if you can.