1

I have a Array

 var Mark = [1,2,3,4,5];

& I want following results after applying javascript

var result=15;

Am using for loop

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

this code correct or not,
I'm sure there is a better way then straight iteration,
anyone please help me

  • Looping is the best way. – vsync Oct 29 '14 at 10:20
  • This code is absolutely correct. For more ways look here http://stackoverflow.com/questions/3762589/fastest-javascript-summation – zavg Oct 29 '14 at 10:20
  • You could make `result= result+ Mark[i];` into `result += Mark[i];`but other than that I don't see any real way to improve upon this. What do you mean by a "better way"? What's wrong with this way? – Calvin Scherle Oct 29 '14 at 10:21

6 Answers6

2

The most fastest way will probably be using a simple loop as you created.

Here's a different approach using Array.Reduce:

var sum = Mark.reduce(function(prev, curr) { return prev + curr; }, 0);
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • 2
    this isn't a better way – vsync Oct 29 '14 at 10:20
  • Why not? It's in the MDN docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#Example:_Sum_up_all_values_within_an_array – Andreas Furster Oct 29 '14 at 10:21
  • so what? you're argument is invalid Andreas. – vsync Oct 29 '14 at 10:22
  • @vsync - Define better..You can do a lot of things with a loop but I'm pretty sure you dont. – Amir Popovich Oct 29 '14 at 10:23
  • 3
    This answer may not be useful, it's still not harmful, I don't understand why this deserves a downvote. – axelduch Oct 29 '14 at 10:23
  • 1
    @vsync, i agree it's not an argument, but "this isn't a better way" isn't a really good argument to – Andreas Furster Oct 29 '14 at 10:25
  • Read this: http://stackoverflow.com/questions/385506/when-is-optimisation-premature – Amir Popovich Oct 29 '14 at 10:26
  • but it is. he asked for a better way. it's obvious as gravity that using a callback function on each array item and having to know what `reduce` does isn't more readable or faster than a plain-old 30+ year old, 2 lines loop – vsync Oct 29 '14 at 10:27
  • @vsync- I agree with that. A straight forward loop will probably be the fastest way to sum up an array. – Amir Popovich Oct 29 '14 at 10:28
  • 1
    Take a look at the code here and you will understand: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce – Amir Popovich Oct 29 '14 at 10:30
  • @KD read the doc too, and learn, don't just look for the answer. – dariogriffo Oct 29 '14 at 10:32
  • @AmirPopovich this is not the fastest way, but I support this as a best approach take a look at these times http://fiddle.jshell.net/8c58vmvn/ even underscore is faster than the array reduce – dariogriffo Oct 29 '14 at 11:27
0

var Mark = [1, 2, 3, 4, 5], sum = 0, l = Mark.length;

while (l--) sum += Mark[l];

console.log('sum is: ', sum);
open console...
0

It's fastest if you cache the array's length and then do a reverse loop on it:

var arr = [1,2,3],
    sum = 0,
    i   = arr.length;

while( i-- )
    sum += arr[i];  // 6
vsync
  • 118,978
  • 58
  • 307
  • 400
  • The OP asked for better, not faster – dariogriffo Oct 29 '14 at 10:43
  • 1
    who are you, his lawyer? please explain why do you think this is not the absolute best way to do this, in your unprofessional opinion. – vsync Oct 29 '14 at 11:10
  • your way is faster and that is not up to discussion, but faster doesn't mean better, if you are so close minded to not see that who is the unprofessional? I repeat what I have already said, my aim is to open the mind of the developer, not to make it a monkey solving problems. – dariogriffo Oct 29 '14 at 11:25
  • lots of words, but this isn't a philosophy class, it's a dead simple question with a single answer. all the other answers aren't better in any way, they are ways to get to a goal, not **the** way. yeah I could have listed here many creative ideas how to achieve the goal, but what's the point, they are all **bad** practices and shouldn't ever be used for such simple things. under the hood they do the same, so why not go straight to the point with minimum code a monkey couldn't understand. always resort to the most basic code if possible. – vsync Oct 29 '14 at 12:03
  • Dude, really? Single answer? you are so closed. I'm sorry for you. – dariogriffo Oct 29 '14 at 12:05
  • nice one, interesting. – vsync Oct 29 '14 at 12:36
  • 1
    @dariogriffo - anyway, he asked for better, not "other". and better != other. – vsync Oct 29 '14 at 15:51
0

This a bit playing with words, but if your array is actually only made of consecutive integers starting from 0 to m or more generally from n to m with a constant common difference d (or step if you prefer), you can do something which is both better and faster ;)

In the case you described

var n = 1;
var m = 5; // (the length of your array)
var d = 1; // you increment with for each element
// n m and d describe [1,2,3,4,5]

var sum = ((m * (m + 1) / 2) - ((n - 1) * n / 2)) * d;

No looping needed, but this works only with matching problems for arithmetic progression

Joke aside, looping is generally a good thing, as long as you don't nest too much of them depending on the same variables boundaries.

axelduch
  • 10,769
  • 2
  • 31
  • 50
-2

I recommend you to take a look to underscore

http://underscorejs.org/

This library will make your live easier in javascript.

And this is how you make a sum with it

var Mark = [1,2,3,4,5];
var sum = _.reduce(Mark, function(memo, num){ return memo + num; }, 0);
dariogriffo
  • 4,148
  • 3
  • 17
  • 34
  • This has nothing to do with underscore.js. It's pure js – Andreas Furster Oct 29 '14 at 10:24
  • 1
    yeah so he should include a whole huge library just to sum up an array? you're kidding me. – vsync Oct 29 '14 at 10:25
  • underscore is pure js, I just made a suggestion form him to start taking a look at something that will help him in the javascript world. – dariogriffo Oct 29 '14 at 10:26
  • @vsync is up to the OP, I just made a suggestion, as I see the OP is new on js, so it will be good to know some things beside manual looping. Better now, than in 1 year. We have to encourage good coding , or that is my aim here. – dariogriffo Oct 29 '14 at 10:29
  • Then why underscore and not loadash? it's much faster. (if we're in business of talking about good practices) – vsync Oct 29 '14 at 10:36
  • @vsync because I wanted to – dariogriffo Oct 29 '14 at 10:42
-2

try this

var result= 0;
 for(var i = 0; i < Mark.length; i++)
 {
    result+= Mark[i];
 }
CSK
  • 777
  • 7
  • 17