Skip to content

Instantly share code, notes, and snippets.

@frentsel
Created September 11, 2018 05:39
Show Gist options
  • Save frentsel/cc3f06f1670bbfddcee5edd20679e4d5 to your computer and use it in GitHub Desktop.
Save frentsel/cc3f06f1670bbfddcee5edd20679e4d5 to your computer and use it in GitHub Desktop.
Recursive function javascript
const data = [1, [2, [3], 4], 5];
const sum = (data) => {
return data.reduce((res, el) => {
if (el.length) {
return res + sum(el);
}
return res + el;
}, 0);
}
alert(sum(data)); // 15
// https://jsfiddle.net/2ary59t0/146/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment