Thursday, September 7, 2023
HomeProgrammingSum an Array of Numbers with JavaScript

Sum an Array of Numbers with JavaScript


It is uncommon that I am disenchanted by the JavaScript language not having a operate that I would like. One such case was summing an array of numbers — I used to be anticipating Math.sum or a likewise, baked in API. Worry not — summing an array of numbers is simple utilizing Array.prototype.cut back!

const numbers = [1, 2, 3, 4];
const sum = numbers.cut back((a, b) => a + b, 0);

The 0 represents the beginning worth whereas with a and b, one represents the working whole with the opposite representing the worth to be added. You may additionally notice that utilizing cut back prevents uncomfortable side effects! I might nonetheless favor one thing like Math.sum(...numbers) however a easy cut back will do!

  • CSS Custom Cursors

    Bear in mind the Internet 1.0 days the place you needed to customise your web site in each method potential?  You abused the scrollbars in Web Explorer, in fact, however the most well-liked exterior service I can bear in mind was CometCursor.  CometCursor allow you to create and use a great deal of customized cursors for…

  • HTML5’s placeholder Attribute

    HTML5 has launched many options to the browser;  some HTML-based, some within the type of JavaScript APIs, however all of them helpful.  Certainly one of my favorites if the introduction of the placeholder attribute to INPUT parts.  The placeholder attribute exhibits textual content in a area till the…


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments