r/javaScriptStudyGroup • u/ForScale • 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
u/Volv Apr 18 '16
1 & 2 done no bother.
Not immediately obvious to me how my fold function helps me make map but I'm sure it will come to me :)
Edit - lol week 143
2
u/ForScale Apr 18 '16
Ha! Something tells me we wont still be doing this a couple years from now, but maybe... :)
2
u/Volv Apr 19 '16
On the theme of challenges / theory / interviews. Found this a few months ago. Fork a copy and see if you can get them all green :) Instructions in source.
Interview Q's
1
u/ForScale Apr 20 '16
That's awesome!
I just spent about an hour and a half on it, got all but three: http://codepen.io/anon/pen/jqKKNv?editors=0010
I want to keep working with it!, but I'm at work so I should probably do some work. Lol!
Thanks for linking it. Did you get all 14?
2
u/Volv Apr 20 '16
Yeh all 14. The last one is the only one that gave me trouble - but remember I've done it once before lol... Who knows how long it took me the first time
1
u/ForScale Apr 20 '16
Nice!
Yeah, I'm struggling to conceptualize my attack on the last one... preliminarily, I'm thinking of having a counter variable and using Object.keys() or maybe one of the newer Object.whatever() that enumerates properties or looks for specific ones or whatever...
Ooh... I just thought of something... I wonder if JSON.stringify() would allow me to do some simple regex matching... BUT that wouldn't involve recursion, so it wouldn't cut it.
...
On the callback one, I feel like I'm not quite understanding the instructions. It says to not modify some code, but then to modify it... and I can easily do function invocations based on conditional flow... I think I'm just confused as to what the challenge is actually looking for on that one.
I'll get back to em later on today! I love doing those kinds of challenges. Thanks again!!
2
u/Volv Apr 20 '16
I'm struggling to think of hints for the last one as it's annoyingly simple and can be done in like 2 lines once you get it.
Still took me over half an hour and about 10 rewrites though lol
The callback one has the wrong instructions I pointed out. You only need to write code inside doWorkWithCallbacks. And the function names are done/fail1
u/ForScale Apr 20 '16
Crazy.
The callback one has the wrong instructions I pointed out. You only need to write code inside doWorkWithCallbacks. And the function names are done/fail
Yeah... okay, that should help when I take a look at it again!
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...
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
1
u/ForScale Apr 20 '16
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...
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 :)
2
u/Volv Apr 20 '16
I did the second thing for the spanish example. I think they are basically equal but I liked smooshing it into 1 liners.
var lookup = { 1: { en: "one", es: "uno" }, 2: { en: "two", es: "dos" }, 3: { en: "three", es: "tres" }, 4: { en: "four", es: "quatro" }, }2
u/ForScale Apr 20 '16
How does one measure elegance (ie, what is an operational definition for elegance?)?
Specific to code, perhaps number of characters/lines where the closer you are to 0, the more elegant it is?
2
u/Volv Apr 20 '16
The elegant part to me was the access by
array[var1][var2]instead of some convoluted switch statement or similar.
Heading out. Back in a few hours.1
u/Volv Apr 19 '16
Started fresh and running through them again right now.
The one "should understand callbacks" has some mistakes in instructions.// re-define the body of doWorkWithReturns so that rather than returning true or false, it invokes a callback function // success for true, and fail for false.should be
// re-define the body of doWorkWithCallbacks so that rather than returning true or false, it invokes a callback function // done for true, and fail for false.1
u/Volv Apr 20 '16
Finished going through them again. That last one tripped me up for way too long.
You might want to replace the 'tile' properties with title as they should be for testing (not required to solve). You'll see what I mean.
Will post my results after/if you have a go :)
2
u/Volv Apr 20 '16
In response to your closure comments - I wrote a crazily wordy set of closure examples based on them.
As always I hope I make sense.
Codepen
1
u/ForScale Apr 20 '16
2
u/Volv Apr 20 '16
read the rest lol.
It explains your problemI can't seem to figure out how to make calls to the same function from different variables increment private/unique variables.
1
u/ForScale Apr 20 '16
Damn... my brain hurts now. Lol!
I think I finally see a use case for closures though! Maybe...
And I still need to work on recursing through objects full of arrays...
Overall, these were great! Thanks a ton for sharing these! My style of learning, I just have to do it over and over and over again until it's beaten in to my mind... that's when things begin to become clear.
So anymore of these you might have, more interview type questions, I'd love to take a crack at em!
Thanks again!
2
u/Volv Apr 20 '16
Was hoping you would maybe know of some lol. Would love to find as many like this as possible. Will dig around some more later.
I'm hoping I can communicate my thoughts properly lol - the important last step I think was the point about the closed over function having it's own copy of the scope at definition.
Fixes a couple of those examples and the last question of the first problem set - did you do that one?1
u/ForScale Apr 20 '16
I forget, did you not like the CoderByte ones that I linked earlier?
There's this: http://codecondo.com/coding-challenges/
the important last step I think was the point about the closed over function having it's own copy of the scope at definition
Yeah... I get that, but it's still not quite clear. We can stop talking about it whenever you want... I'm sure you're getting tired of it. I've got this now: http://codepen.io/anon/pen/zqaMWP?editors=0010
I didn't do the first question of the last problem set yet.
2
u/Volv Apr 20 '16
You only get one result from your alternative solution.
Codepen1
u/ForScale Apr 20 '16
Okay... that is crazy that x doesn't get reset to 1 in each new call with the closure...
→ More replies (1)1
u/Volv Apr 20 '16
CoderByte
I couldn't look at any beyond the first few without a membership? Unless I'm missing something. Will try completing a few see if they unlock
1
u/ForScale Apr 20 '16
Oh... weird... I guess that's new. A year or so ago I was able to do the medium ones; now it's asking for membership.
2
u/Volv Apr 20 '16
And did you like rewrite - crazy short edition.
I know doing that kind of thing can be anti team and sometimes hard to read.. but makes me happy lol :)
1
u/ForScale Apr 20 '16
Yes, I did like it! I like the extremely short code/syntax! Makes me happy too!
1
u/ForScale Apr 21 '16
Found these:
Define a repeatify function on the String object. The function accepts an integer that specifies how many times the string has to be repeated. The function returns the string repeated the number of times specified. For example:
console.log('hello'.repeatify(3));Should print hellohellohello.
...
var fullname = 'John Doe'; var obj = { fullname: 'Colin Ihrig', prop: { fullname: 'Aurelio De Rosa', getFullname: function() { return this.fullname; } } }; console.log(obj.prop.getFullname()); var test = obj.prop.getFullname; console.log(test());Fix the previous code so that the last console.log() prints Aurelio De Rosa.
...
And all of these (obviously haven't looked through all of it): http://www.w3resource.com/javascript-exercises/
2
u/Volv Apr 21 '16
2
u/ForScale Apr 22 '16
Lol! I was thinking we could use them to put together something for next week. :) But excellent work!
1
u/ForScale Apr 25 '16
Hey, happy Monday!
What do you want the focus to be this week?
I thought we could do 1) some more interview style questions 2) closures round 2 3) regExp 4) ajax/API 5) promises round 2 6) or anything you want!
Let me know...
1
u/Volv Apr 25 '16
As many interview questions as can be found, happy to talk closures too :)
2
u/ForScale Apr 25 '16
Okay, well... How about these? http://www.w3resource.com/javascript-exercises/javascript-object-exercises.php Do em throughout the week?
And with closures, I get it now: http://codepen.io/anon/pen/WwgGZo?editors=0012 But my knowledge seems kind of limited to using numerical data. Can we try some closure examples with strings and perhaps data structures like arrays/objects?
What do you think about making the questions/challenges the focus for the week, and then we can just keep discussing closures on the side? Sound good?
→ More replies (0)
2
2
u/ForScale Apr 18 '16 edited Apr 19 '16
ENTRY
http://codepen.io/anon/pen/dMeBKr
At this point, first two are done. I got a little confused on the input for the second one, but I made it so the given input spits out the desired output.
*Got three done... even more confused as to what it's looking for with regard to input/output...
http://codepen.io/anon/pen/oxyzEX?editors=0012http://codepen.io/anon/pen/qZKrBv?editors=0012 Still struggling with using that fold() in map()...