Cloudboy22
Cloudboy22

Reputation: 1514

The sum of all the elements in an Array

i am trying to do some examples. i want to sum all the elements of the array here is my code

var marks = [10, 20, 30, 40];
var total = 0;

for (var singleMark in marks) {
    total += marks;

}
console.log("the total marks re " + total);

But the results displayed is the total marks re 010,20,30,4010,20,30,4010,20,30,4010,20,30,40

Please help me and tell me where am i doing it wrong.Thanks. I can take the for loop but i want to learn using foreach as in Javascript the definite guide book it says so

Upvotes: 1

Views: 4708

Answers (8)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

try like this

var marks = [10, 20, 30, 40];
var sum=marks.reduce(function(a, b){return a+b;})

From @thomas comment

ES6 syntax you can also write:

[10, 20, 30, 40].reduce((a,b) => a + b)

Upvotes: 5

Adithya Santhosh
Adithya Santhosh

Reputation: 54

Its Easy.

Create a function with a parameter.

Inside the function loop through the array Till the loop is finished.

Inside the Loop add the elements.

function sumArray(arra){

    var sum=0;
    for (var i = 0; i <arra.length; i++) {
        sum= sum+arra[i];
    }
}

Upvotes: 0

Mystical
Mystical

Reputation: 2783

Here's a more high-order and readable approach:

marks.reduce(Math.sum, 0);

The reduce function is used along with the Math.sum and does this: foreach element of marks, use math.sum(cur, marks[i]); to take the current element and the next one; do this until all of the elements in the array have been passed to the sum function and have been added up like this. Hope this helps.

Upvotes: 1

Ling Zhong
Ling Zhong

Reputation: 1804

You're just using the wrong variable marks instead of singleMark in your loop. Try this:

for (var singleMark in marks) {
    total += marks[singleMark];
}

Edit: However it is bad practice to use in to iterate through arrays. Consider using javascript Array.prototype functions like .reduce() or .forEach()

Upvotes: 0

Max
Max

Reputation: 2806

For in will go over properties of objects. If you where to use for in on an array it will return you the index it is iterating over.

You should be using forEach to iterate over arrays.

marks.forEach(function(element, index){
    total+=element;
});

The element value is the element from the array, while the index value is the index that forEach is currently at. index is optional and does not need to be included.

Upvotes: 5

Ashokreddy
Ashokreddy

Reputation: 352

you can use the following two ways

var marks = [10, 20, 30, 40];
        var total = 0;
        marks.forEach(function addNumbers(value) {
            total += value;
        });
        console.log(total);

or
var marks = [10, 20, 30, 40];
        var total = 0;
        var singleMark;
        for (var i = 0; i < marks.length;i++) {
            total = total + marks[i];

        }
        console.log("the total marks re " + total);

Upvotes: 1

Rayon
Rayon

Reputation: 36609

Why don't you use conventional for loop ?

var marks = [10, 20, 30, 40];
var total = 0;
for (var i = 0, iLen = marks.length; i < iLen; i++) {
    total += marks[i];
}
console.log("the total marks re " + total);

You can use reduce too. The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.

var total = [0, 1, 2, 3].reduce(function(a, b) {
  return a + b;
});
// total == 6

Upvotes: 2

Harshal Carpenter
Harshal Carpenter

Reputation: 508

Do not use for .. in for traversing in array. Use for(..;..;..)

The values are represented as strings in this way.

For reference, safer way:

for(var i = 0; i< arr.length; i++){
    sum += arr[i];
}

Upvotes: 1

Related Questions