DEV Community

Daniel Mutwiri
Daniel Mutwiri

Posted on

LUX TECH ACADEMY- Data Structure and Algorithms Week 1

https://www.hackerrank.com/challenges/simple-array-sum/problem?isFullScreen=true

Solutions
Solution 1.

function simpleArraySum(ar) {
    // using a forEach or a for loop
   let sum = 0;
   ar.forEach((element) => {
      sum = sum + element;
   });
  return sum;
 }
Enter fullscreen mode Exit fullscreen mode

Solution 2.

function simpleArraySum(ar) {
    // Using a inbuilt function reduce.
    return ar.reduce((a, b) => a+b, 0);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)