How to sum every number in an array of numbers.

Using the ES2015 syntax it can be done as follows: let numbers = [10, 42, 5, 87, 61, 34, 99]; let sum = numbers.reduce((a,b) => a + b); where the reduce signature is Array.prototype.reduce(accumulator, element, callback, startingValue) alternatively a for loop could be used.

MC

Related Javascript Mentoring answers

All answers ▸

If I do var a=3 and var b=4 and then do var c=a+b it tells me the answer is 34. Why?


Suppose that you have a <p> element with an id of 'id1'. Use javascript to set the inner html of this element to 'hello!'


Make button that says how many times it has been clicked.


I've just heard about objects. How do I use them and do they work with Arrays?