-3

I have the following:

var numSum = function(set) {
  for(i = 0, i < set.length, i++) {
      var arrayValue = set[i];
      var totalValue = arrayValue + 
  }
}
numSum([1, 2, 3, 4]);

But all I get back is

Uncaught Syntax Error

. What am I doing wrong?

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
kmgiffin
  • 39
  • 5
  • 1
    `total = set.reduce (function (a, b) { return +a + b;});` – Jaromanda X Dec 14 '15 at 06:57
  • 1
    Duplicate of http://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers and http://stackoverflow.com/questions/3762589/fastest-javascript-summation – Zorgatone Dec 14 '15 at 07:55

6 Answers6

4

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

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
1

The following changes are required:

  1. initialize totalValue outside the loop
  2. use the increment-assignment operator correctly

It should be

var numSum = function(set) {
  var totalValue = 0;
  for(i = 0, i < set.length, i++) {
      totalValue += set[i];
  }
}
numSum([1, 2, 3, 4]);
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
1

You can Do it Like this.

var numSum = function(set) {
    var arrayValue = 0;
    for (i = 0; i < set.length; i++) {
        arrayValue += set[i];

    }
    return (arrayValue);
};
console.log(numSum([1, 2, 3, 4]));

So You need to change few Things in urs code

  1. Structure of For Loop
  2. use of Assignment operator. it should be like x+y or x+=y

few improvment

  1. You can use only one variable to store sum and can return that.
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Roli Agrawal
  • 2,356
  • 3
  • 23
  • 28
0

There are typo mistakes

var numSum = function(set) {
  for(i = 0; i < set.length; i++) {  // replace ',' to ';'
      var arrayValue = set[i];
      var totalValue += arrayValue; //  += operator is short of totalValue = totalValue  + arrayValue
  }
}
numSum([1, 2, 3, 4]);
ozil
  • 6,930
  • 9
  • 33
  • 56
-1

You got "Uncaught Syntax Error" for this line

var totalValue = arrayValue + 

this should be

totalValue += arrayValue;

Your numSum function will be like

function numSum (set) {
  var totalValue = 0;
  for(i = 0, i < set.length, i++) {
    totalValue += set[i];
  }
  return totalValue;
}
numSum([1, 2, 3, 4]);
Scarecrow
  • 4,057
  • 3
  • 30
  • 56
-1
Try this:
var numSum = function(set) {
                var arrayValue = 0;
                for(i = 0 ; i < set.length ; i++) {
                     arrayValue += set[i];                    
                }
                return arrayValue;
            }
            alert(numSum([1, 2, 3, 4]));
Meenakshi
  • 165
  • 1
  • 8