DEV Community

Cover image for Simple Array Sum –[HackerRank Coding Challenge]
Ghameerah McCullers
Ghameerah McCullers

Posted on

Simple Array Sum –[HackerRank Coding Challenge]

Warning, it is assumed that if you are reading this you understand JavaScript ES6 concepts.

Problem

Given an array of integers, find the sum of its elements.

For example, if the array [1,2,3], 1 + 2 + 3 = 6 , so return 6.

Function Description

Complete the simpleArraySum function in the editor below. It must return the sum of the array elements as an integer.

simpleArraySum has the following parameter(s):

ar: an array of integers

Solution

const simpleArraySum = arr => arr.reduce((acc,curr) => acc + curr, 0) 
Enter fullscreen mode Exit fullscreen mode

Read more on the reduce() JavaScript ES6 array method here => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

P.s. here's another really cool resource I like for learning more about JavaScript and reduce() => https://www.youtube.com/watch?v=-LFjnY1PEDA&t=401s&ab_channel=TheCodingTrain

Happy Coding!

Top comments (0)