A super easy code challenge
This past week I applied for a job, had a great first round chat, and was sent a code challenge. Simple JS, some files provided, some basic methods for me to complete, no biggie. Let me send it right back to y — oh.
Lots of code, a detailed README, a deck of cards needs to be shuffled and dealt to several players, the details of how to achieve both were up to me. I was told there was no rush to return the challenge, so I took my time combing through the files, read every line of code, took some notes on paper (I know!), and started to play around with it a bit. I felt pressure to complete it as quickly as possible but made an effort to take my own advice and not rush something that doesn’t need to be rushed. Thanks to my impeccable timing I first opened this challenge on the morning of Election Day, ended up volunteering most of the day (you’re welcome, America, I’m not even from here), and anxiously refreshing Twitter all night (I’m not even from here!!), and finally got to dive in the next day. I started playing around with the code to figure out how to best approach it without googling any tips and tricks (I have repeatedly been admonished at for this by a senior dev friend, but I like to at least try first to see what my brain comes up with on its own, dammit). I started manipulating the provided deck with basic array functions, using the indexes to swap cards (which technically counts as shuffling?), but that wasn’t particularly elegant or even particularly shuffly.
After a few attempts, I caved and looked around for randomized sorting algorithms to get a better idea of how to approach this. I’m in the early days of working with algorithmic logic so I took this as an opportunity to learn a whole lot about this particular problem. The biggest issue is to force a truly random shuffle, not to just swap two cards with each other or putting them in designated places.
That’s where Math.random() comes in and provides us with a random float between 0 (including zero) and 1 (excluding 1) and we learn about the Fisher-Yates algorithm. Read all about it here if you’d like, it’s a good one. Took me a few takes to really understand it and be able to step through it but it was very worth the time.
I wrote a simple deal method, refactored a bit, played around with it for a while, and confirmed it was working as planned after dropping it into a React sandbox and working on some of the basics there. Since all the methods had been taken care of, I was left with just the tests to work on. Part of the code challenge was getting all my code unit tested with Jest, with some tests already provided and some I’d need to write myself. But, as luck would have it, the entire test suite failed to run, wouldn’t even get to testing my code, just failed big time altogether.
Maybe if I run it again? Probably not. Let’s do it anyway…
UGH. But at least it’s behaving exactly es expected. Progress.
Ok, so, what’s happening here… googled… aaand… Nice. StackOverflow hit.
Sweet. Easy. Turns out a bunch of people were having this issue, seemed to be an easy fix, combed through the jsdom, tried a few approaches, no luck. Searched more, googled more, even asked my old teacher… nothing. Nobody had ever even seen this. I’m supposed to write functions and tests to demonstrate my technical prowess and I can’t even figure out how to get the tests to run. Suboptimal.
- Try to make it all work without tests and just submit the logic, untested, play it cool. Maybe they won’t notice? 2/10
- K̶e̶e̶p̶ ̶g̶r̶i̶n̶d̶i̶n̶g̶. It’s Day 4. Forget this option. 0/10
- Ask for help. Either they’ll think I’m not so bright or a quitter and I won’t get the job or I’ll get help and get the job, maybe some wicked combination of both. 5/10
Option 3 wins. Sounding as professional and technically versed as possible, I emailed my status report, breaking down all the ways in which I’d try to fix the issues, adding that I didn’t want to ask right away in case this was all part of the challenge. Promptly got an email back saying “Oh, that’s my bad, this one has an old version of Jest that’s not compatible with your machine… I’ll push the updated one right over. Ha, would have been funny had you caught that and lectured me about it.” Pull. Install. Test. Pass. Whew.
Problem solved, crisis averted, faith restored, all systems go. Easy peasy-ish. Sometimes you just gotta ask, I guess.