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

4
  • 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. Commented Apr 4, 2013 at 7:46
  • Really? Because each of those answers explicitly answer your question. May I ask in what way they failed to solve your problem? Commented Apr 4, 2013 at 7: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 Commented Apr 4, 2013 at 7: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. Commented Apr 4, 2013 at 7:57

3 Answers 3

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; }); }
Sign up to request clarification or add additional context in comments.

4 Comments

+1 Superb usecase for reduce, however you should note that it does not support IEs < 9 which atm is still a big hit.
Overwriting the Array prototype for IE is left as an excercise for the reader ;)
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.
Yeah, like the initial question.
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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.