r/javascript • u/Elektryk91 • Mar 08 '19
How I ruined my JavaScript code and still won the Coding Challenge
https://tsh.io/blog/how-i-ruined-my-javascript-code-still-won-coding-challenge/5
4
u/maxoys45 Mar 08 '19
Really nice article, but as others have mentioned, my first thought was "what about exponents!".
4
u/R3DSMiLE Mar 08 '19
I... I like how you think. Simple solutions for a Coding Challenge just don't seem right - I always think that these challenges are meant to check how much you can bend the tech you chose without breaking it and making it do what you want to :)
4
u/atubofsoup Mar 08 '19
The coding challenge itself seems strange. Aside from the binary/hex gotchas, it's not even a difficult/interesting problem unless you specify "no regexes" (or you're presenting it to regex beginners).
1
u/R3DSMiLE Mar 10 '19
The coding challenge itself might be suffering from the "we ran out of ideas, but this shit is fun" and that's probably why it's not even a difficult one, or since it's a company environment the challenges can't be too hard or would take focus away from your project
3
u/thegrandechawhee Mar 08 '19
Great read. Had a good laugh at the distracted boyfriend meme! As some one who struggles with regular expressions i applaud you.
6
u/atubofsoup Mar 08 '19
I use to pride myself on being able to find convoluted imperative solutions to problems that should be solved with regexes because I hated/didn't understand regexes. Then one day regexes just clicked, and I started using them everywhere. Some days I wonder what poor soul has to deal with those nested loops I left behind.
1
u/Arkhenstone Mar 08 '19
I remember my coworker going full scale regex to convert a pascal format (LFM) to JSON. Took him a day. Then noticed a function solved that already.
0
Mar 08 '19
[deleted]
2
u/mtsuggs Mar 09 '19
In my experience you can solve a problem with regex but now you have a new problem...the next dev who needs to maintain or enhance that regex.
1
Mar 09 '19
It takes all of a weekend to master regex, maybe stop hiring drama majors and you'll be all right.
1
u/mtsuggs Mar 09 '19 edited Mar 09 '19
Yeah go ahead and learn how to enhance this over the weekend for some custom business rule. http://ex-parrot.com/~pdw/Mail-RFC822-Address.html
The point I was trying to make was readability and maintainability are factors in a great solution not just efficiency and lines of code.
1
u/atubofsoup Mar 09 '19
I think at some point I spent a couple hours doing some online regex challenges. I've also used them quite a bit writing random scripts/apps. At one point I got the idea to build a regex builder library to make writing regexes more readable, but by the time I wrote the library I didn't really need it to comfortably read regexes, so I never finished/released/used it.
I wouldn't call myself an expert though. I still get caught up on nested matches and look ahead/behind stuff.
4
Mar 08 '19
My boss and I frequently compete with regex in our code. Check out regexper it is very helpful even if you have been working with them for years.
Here are a couple of not overly complicated expressions but fairly long, they're for matching DMS lat/lng coords. Put them in regexper and they go from WTF to AH HA!
DMS.latitude - /^[\+-]?(([1-8]?\d)\D+([1-5]?\d|60)\D+([1-5]?\d|60)(\.\d+)?|90\D+0\D+0)\D+[NSns]?$/ DMS.longitude - /^[\+-]?([1-7]?\d{1,2}\D+([1-5]?\d|60)\D+([1-5]?\d|60)(\.\d+)?|180\D+0\D+0)\D+[EWew]?$/1
0
Mar 09 '19
This is a perfect example of what not to do with regex. Just parse the numbers and validate them with what we know about coordinate values. Use common sense, in other words.
1
3
2
u/F13_on_da_Wa11 Mar 08 '19
Great story, and very good at illustrating a number of JavaScript number and operator features.
2
u/rebl_ Mar 08 '19
I would rather write the most simple code possible(regex) than winning a contest.
2
u/Slypenslyde Mar 08 '19
My favorite part of this is even though the link has no clear anchor within it, once the page loads my browser jumps down to about the 80% mark of the article.
2
u/nimbledaemon Mar 09 '19
This is great for code as art. If you ever tried to use this in production code, it should be taken out back and shot.
3
1
1
1
u/fagnerbrack Mar 19 '19
A perfect Test-Driven Development approach
The author doesn't have any idea of what TDD is and didn't use the power of it to help develop the code. He wrote 12 failing tests instead of using one test to drive the next step of the implementation in a red/green/refactor cycle. Golfing in the refactoring step after you write the code like this is much safer, faster, and allows the author to understand better the boundaries of the problem to come up with better and more efficient golf ideas. He probably missed a lot of opportunity here.
See also You Don't Know TDD for an example of TDD in JavaScript (not code golf per se, but you can easily refactor to it).
We’re not done yet because our function is currently returning the number, but it has to return a TRUE or FALSE value
Another part that clearly shows the author did not use the test to drive the interface (output) of of the function. At this step he made a random decision to switch the output to a different type without having a test to drive him to do it.
Before saying it's a "perfect" TDD approach, make sure you know what you're talking about in the first place or edit the post to avoid misleading other people. It's because of posts like this that newbies believe learning TDD is a waste of time.
0
Mar 08 '19
[deleted]
14
u/Rainbowlemon Mar 08 '19
I noticed this! Dealt with binary, but not exponents. Oops! Still enjoyed the read :)
5
u/Arkhenstone Mar 08 '19
Noticed it also, but hey, there's no flaws in a code you write test by yourself ;)
2
u/itspedruu Mar 08 '19
v=c=>!!(c[4]&&+('1.'+c.slice(0,2)+'e'+c.slice(-3))&&['',...' -_'].includes(c.slice(2,-3)))Adding a
eafter thec.slice(0,2)would solve it0
2
Mar 09 '19 edited Mar 09 '19
!!str.match(/\d\d[ _-]?\d{3}$/) I can't imagine anyone thinking this is better or easier without regex. Kids these days (grumble, grumble) Also reddit pleasse fix ^ (caret) formatting you twats
1
1
u/gillythree Mar 09 '19
Can't even properly format a reddit post (grumble, grumble) ...
/^\d\d[ _-]?\d{3}$/.test(s)Kids these days!
0
Mar 08 '19
[deleted]
28
Mar 08 '19
Ironically, I see it most often on webdev-centric sites.
18
u/crazedizzled Mar 08 '19
We're lazy.
11
u/RodrigoBAC Mar 08 '19
Shoemaker's son always goes barefoot
2
u/uneditablepoly Mar 09 '19
I know it as "the cobbler's children wear no shoes," and I find myself saying it all the time in this field.
1
8
Mar 08 '19
I read it fine on iOS safari...
Edit: the repl.it at the bottom was all over the place, but I had no issues with the text and gists.
3
14
u/[deleted] Mar 08 '19
mad lad