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.


Image description


- 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)

puckfried profile image

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

abusalehfaysal profile image
Abu Saleh Faysal Author

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

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.