DEV Community

Cover image for HackerRank — Problem Solving — JavaScript — Simple Array Sum
Abu Saleh Faysal
Abu Saleh Faysal

Posted on

HackerRank — Problem Solving — JavaScript — Simple Array Sum

Image description

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:

Image description

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)

Collapse
 
puckfried profile image
puckfried

Why not using reduce() function, so your 4 steps would fit in one line:
let sum = yourArray.reduce((acc, curr)=>acc+curr);

Collapse
 
abusalehfaysal profile image
Abu Saleh Faysal

Thanks for your efficient solution. Will try it next time.