Given an array of integers, find the sum of its elements. For instance, if the array=[10,20,30], then it will return 10+20+30 = 60.
Solution:
Explanation:
- Step 01: Take a variable named sum and store an initial value as 0.
- Step 02: Iterate a for loop through the given array.
- Step 03: Add up each array element in the sum variable.
-Step 04: Return the sum variable after adding all the array element.
Top comments (2)
Why not using reduce() function, so your 4 steps would fit in one line:
let sum = yourArray.reduce((acc, curr)=>acc+curr);
Thanks for your efficient solution. Will try it next time.