For loop need ;
not ,
It should be
for(i = 0; i < set.length; i++)
Shortcode of x=x+y
is x+=y
.
But you are doing it in wrong format.
It should be
totalValue += arrayValue
If you declare variable inside loop it'll create every time with the loop.
declare it outside of the loop.
Like this
var totalValue=0;
for(i = 0; i < set.length; i++) {
var arrayValue = set[i];
totalValue+= arrayValue ;
}
you don't need to declare extra variable to hold.
Try like this
var numSum = function(set) {
var totalValue=0;
for(i = 0; i < set.length; i++) {
totalValue += set[i];
}
return totalValue;
}
numSum([1, 2, 3, 4]);
JSFIDDLE
You can do it using Array.prototype.reduce().
Try like this
var sum = [1, 2, 3, 4].reduce(function(prev, curr) { return prev + curr; });
JSFIDDLE