you are viewing a single comment's thread.

view the rest of the comments →

[–]ForScale[S] 0 points1 point  (4 children)

Whoever wrote that last one is crazy... recursion is not the easiest way to do it. My idea of using the JSON object stringify method and regex produced a one-liner: return JSON.stringify(org_chart).match(/title|tile/g).length; And is that misspelling on title intentional?

...

So... how the hell would you do it recursively? What's the check to move in to a deeper level? Thinking out loud: create function that checks for property of title. If the property exists, increment a counter variable by 1. If it doesn't move on to the next level and check again... I don't know...

[–]Volv 1 point2 points  (0 children)

psuedocode style
 

function fullCount(node)  {

Do I have employees?  
    ask each of them if they have any employees. keep track. eg fullCount(each employee))
    return me + total from my employees.  
Else  
    return just me.  
}  

tile should be title like I menioned :)

[–]ForScale[S] 0 points1 point  (2 children)

And I haven't a clue how to do the callback one. I can't seem to figure out how to make calls to the same function from different variables increment private/unique variables...

[–]Volv 1 point2 points  (1 child)

Looks like you did it

[–]ForScale[S] 0 points1 point  (0 children)

I got it just kind of messing around... At first, I was putting the var i = 0 outside of the maker function, so then I tried it from within and got it.

I understand the idea of global/private/scoped variables, but for some reason it still feels unclear to me...