DEV Community

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

Posted on

16 1

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.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay