Skip to content

Instantly share code, notes, and snippets.

@jhgaylor
Created June 13, 2016 16:55
Show Gist options
  • Save jhgaylor/4591ccc393f8f0bbc315b7fb4b879de7 to your computer and use it in GitHub Desktop.
Save jhgaylor/4591ccc393f8f0bbc315b7fb4b879de7 to your computer and use it in GitHub Desktop.
Does your brain hurt when you think of trees? Mine too... I don't ever want to write this again.
function countChildren (node) {
function _countChildren (node) {
if (node.children) {
return node.children.map((child) => {
return _countChildren(child);
}).reduce((memo, next) => {
return memo + next;
}, 1);
}
return 1;
}
return _countChildren(node) - 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment