logic - How to add up numbers in a nested array javascript -
just working on project , tried few different solutions no results. me how add numbers in nested array? use reduce? or loop?
function balance(arr) { if(typeof item == 'number') { return arr;enter code here } else { return arr + balance(item); } }
is maybe hoping for?
function balance(arr) { return arr.reduce(function(sum, item) { if(typeof item == 'number') { return sum; } else { return sum + balance(item); } },0); } console.log(balance([1,2,[3,4],5]));
Comments
Post a Comment