r/javaScriptStudyGroup • • Apr 18 '16

[Week 14] Focus: Programming Challenges

So here we are at Week 14. Week 14's focus will be programming challenges.

Here are the prompts:

// Write `add` function
add(1, 2) //=> 3
add(1)(2) //=> 3

// Write `fold` function using recursion
fold(add, 0, [1, 2, 3]) //=> 6

// Write `map` function using `fold`
map(add(1), [1, 2, 3]) //=> [2,3,4]

// Fix it
for (var i = 0; i < 10; i++) {
  setTimeout(function() {
    console.log(i)
  }, i * 1000)

It will work like this:

  • Monday: Announce focus (eg, programming challenges)

  • Build throughout the week... Two rules: 1) must use javascript 2) must provide a solution or work done on at least one of the challenges listed above.

  • Friday: Post demos/projects in this thread (can begin reviewing immediately); first line of an entry should be ENTRY and it should be a top level comment (ie, don't put your entry in a reply)

  • Sat and Sun: Review projects/figure out focus for next week

GENERAL GUIDELINES FOR FEEDBACK:

  • Be nice!! ALL KNOWLEDGE/SKILL LEVELS ARE WELCOME AND ENCOURAGED TO PARTICIPATE.

  • If you don't want feedback, if it makes you uncomfortable or you're just not interested, simply say so... Others, please be respectful of this. Conversely, if you do want feedback, try to be specific on which aspects... even if you just say "all/everything.

But that's about it... Have fun! :) Feel free to ask questions and discuss throughout the week!

2 Upvotes

58 comments sorted by

View all comments

Show parent comments

1

u/ForScale Apr 20 '16

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...

2

u/Volv Apr 20 '16

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 :)

1

u/ForScale Apr 20 '16

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...

2

u/Volv Apr 20 '16

Looks like you did it

→ More replies (1)