Skip to content

Commit

Permalink
Add array sum problem for JS language
Browse files Browse the repository at this point in the history
  • Loading branch information
ihsansatriawan committed Oct 21, 2018
1 parent 9bde9de commit 1530174
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions problems/array-sum/array-sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function arraySum(arr) {
let sum = 0;
const slicedArray = arr.slice();
const dimension = 3;
let flatArray = slicedArray.reduce((acc, val) => acc.concat(val), []);

for (let i = 0; i < dimension; i++) {
flatArray = flatArray.reduce((acc, val) => acc.concat(val), []);
}

flatArray.forEach((element) => sum += element)
return sum;
}

console.log(arraySum([1, 2, [3, 4, [5]]]));

0 comments on commit 1530174

Please sign in to comment.