-1

I really apologize if this has been asked before, but I've been searching/trying on my own for the last 4 hours and have nothing.

I am trying to figure out how to create a function in Javascript that will give me back the sum of all the numbers contained inside an array (i.e. [12, 50, 3] would give back 65.I need to be able to compute the sum of any sized array.

I know I'm a total noob at this, but this is what I have so far:

var findSum = function() {    
}
var n = [10, 12];
findSum(a);

Won't be able to explain how grateful I will be for any help.

PS If you're browsing this, hello, Professor ;)

coolguy
  • 7,866
  • 9
  • 45
  • 71
  • To elaborate I simply cannot figure out what sort of code I should be placing in the function. I don't even know if I have a reference point to give you, because I am simply clueless right now. I've gone through the slides and my notes from class, and I am not having any luck. – Chris Hoffman Apr 04 '13 at 07:46
  • Really? Because each of those answers explicitly answer your question. May I ask in what way they failed to solve your problem? – David Thomas Apr 04 '13 at 07:48
  • Then I apologize for any reposted questions. I'm just getting really frustrated and desperate right now. I'll go through that thread again – Chris Hoffman Apr 04 '13 at 07:51
  • I'm not overly concerned about the duplication, I'm more interested in how they might fail to answer your question, since that might sufficiently differentiate your question to avoid yours being closed. *If* those answers *don't* answer your question, or fail to solve your problem, *please* explain how they fail, and we, and *I*, will happily try to help. I'm sorry if it seems like I'm unduly criticising or penalising, that truly isn't my intent. – David Thomas Apr 04 '13 at 07:57

3 Answers3

3

This is a standard example for reduce straight from the documentation: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/Reduce

[0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){
    return previousValue + currentValue;
});

Thus

findSum(a) { return a.reduce(function(a, b) { return a+b; }); }
Udo Klein
  • 6,784
  • 1
  • 36
  • 61
  • +1 Superb usecase for reduce, however you should note that it does not support IEs < 9 which atm is still a big hit. – Christoph Apr 04 '13 at 07:41
  • Overwriting the Array prototype for IE is left as an excercise for the reader ;) – Udo Klein Apr 04 '13 at 07:46
  • Not much of an exercise, since that's featured on the page to which you link; incidentally Christoph didn't ask you to *do* that, just note in your answer that anyone implementing this answer may/will have to take compatibility into account. – David Thomas Apr 04 '13 at 07:51
  • Yeah, like the initial question. – Udo Klein Apr 04 '13 at 10:47
0

Try this one

var array = [1,3,4,5,2,10];
var sumOfArray = 0;
array.forEach(function(value,index){
     sumOfArray += value;
 });
console.log(sumOfArray); //25

Fiddle Demo

karthick
  • 5,998
  • 12
  • 52
  • 90
0

A native Javascript implementation in it's simplest form without compatibility problems:

function findSum(arr){
   var sum=0;
   for(var i = arr.length;i--;){
      sum += arr[i];
   }
   return sum;
}

// or
function findSum(arr){
   var sum=0,i=arr.length;
   while(i--){sum += arr[i];}
   return sum;
}

findSum(a);
Christoph
  • 50,121
  • 21
  • 99
  • 128