-5

I'm having a competition with my friend, he told me to- Create a function called addThemAllTogether that takes in an array of numbers and returns the total of all of the items in the array added together.

What does that look like in Javascript?

  • 1
    You use a text editor to write it? Sorry, but it's unclear what exactly you are asking for. If you are actually asking how to sum an array, please [use the search](https://stackoverflow.com/search?q=[javascript]+sum+array+elements). – Felix Kling Oct 21 '15 at 04:03
  • Sorry for being unclear. Here is the question verbatim- Create a function called addThemAllTogether that takes in an array of numbers and returns the total of all of the items in the array added together – novice at best Oct 21 '15 at 04:05
  • 1
    That doesn't sound like a question but like an assignment. I assume the whole purpose of the assignment is that **you** solve it. We gladly help you if you have a specific question or you are stuck, but we certainly don't do the assignment for you. – Felix Kling Oct 21 '15 at 04:08
  • I assume you know how to define a function, how to assign a value to a variable and how to add two values. So maybe you are stuck at how to iterate over the array. Like in many other languages, you can use a [`for` loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for) for that. If you don't know anything about the other things, I suggest to read http://eloquentjavascript.net/. – Felix Kling Oct 21 '15 at 04:11

2 Answers2

2

You don't, JavaScript already has that function, and it's called Array.prototype.reduce:

// set up some list of values
var list = [1,2,3,...,999,1000];

// run through the array, updating a tally value for every element we see:
var sum = list.reduce(function(runningTally, currentValue) {
  // simplest possible thing: add the current value to the tally,
  // which means at the end of the iteration the tally will the sum
  // of all elements in the array.
  return runningTally + currentValue;
}, 0)

The second argument "0" is the start value for the running tally. It's optional, but usually a good idea to explicitly set.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • I think you should have rather closed this question as duplicate of [How to find the sum of an array of numbers](http://stackoverflow.com/q/1230233/218196) – Felix Kling Oct 21 '15 at 04:31
  • not quite sure why you would tell me that and leave a long comment on the question, instead of just adding your vote to close the answer as duplicate and have others follow that vote? I guess we were both feeling more helpful than when we just vote to close and move on to the next question. – Mike 'Pomax' Kamermans Oct 21 '15 at 04:32
  • Because I didn't feel like the question is worth answering. Hence I just provided some hints and voted to close it as "too broad". However, if someone decided that it is worth answering, it would be more appropriate to close it as duplicate, which it clearly is. – Felix Kling Oct 21 '15 at 04:38
0

cycle through the array with a for loop

var array = [1,2,4,223,53,6,1];
var total = 0;
for( i = 0; i < array.length; i++ ) {
       total += i;
}

working code