DEV Community

Daniel Mutwiri
Daniel Mutwiri

Posted on

4 3

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay