Skip to content

Commit

Permalink
Complete treeCountLeaves.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Jan 1, 2015
1 parent a2a5043 commit fcc4672
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions treeCountLeaves/treeCountLeaves.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,20 @@ var Tree = function(value){
};

Tree.prototype.countLeaves = function () {
// TODO: implement me!
}
// check if 'this' tree instance has any children
// if no, return 1
// else
// create a result variable and set to 0
// for each child, execute countLeaves on child
// append each count to result
// return result
var len = this.children.length;

if (len === 0) return 1;
return this.children.reduce(function (acc, currentChild) {
return acc + currentChild.countLeaves();
}, 0);
};

/**
* You shouldn't need to change anything below here, but feel free to look.
Expand Down

0 comments on commit fcc4672

Please sign in to comment.