admin管理员组

文章数量:1290954

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

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

Share Improve this question edited Oct 18, 2015 at 2:33 Cloudboy22 asked Oct 18, 2015 at 2:22 Cloudboy22Cloudboy22 1,5145 gold badges22 silver badges41 bronze badges
Add a ment  | 

8 Answers 8

Reset to default 5

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.

try like this

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

From @thomas ment

ES6 syntax you can also write:

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

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

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];
}
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);

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.

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()

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];
    }
}

本文标签: javascriptThe sum of all the elements in an ArrayStack Overflow