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;
}
Solution 2.
function simpleArraySum(ar) {
// Using a inbuilt function reduce.
return ar.reduce((a, b) => a+b, 0);
}
Top comments (0)