// 3. Sum all numbers in an array containing nested arrays.
// arraySum([1,[2,3],[[4]],5]); // 15
var arraySum = function(array) {
var result = 0;
if(! Array.isArray(array)) {
return array;
} else {
array.forEach(arr => {
result += arraySum(arr);
})
}
return result;
};
console.log(arraySum([1,[2,3],[[4]],5]));
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter