Skip to content

Instantly share code, notes, and snippets.

@hhoopes
Last active May 6, 2016 22:28
Show Gist options
  • Save hhoopes/63aa659df7ac604438d4adcfc6e59a70 to your computer and use it in GitHub Desktop.
Save hhoopes/63aa659df7ac604438d4adcfc6e59a70 to your computer and use it in GitHub Desktop.
A comparison of my JS exercisms, comparing them to the solutions of other coders

Leap

My Solution

  1. (Link) I was interested in her solution because she specifically called out not liking lots of nested if loops, and I tend to agree, and felt like that was a problem with mine. We basically went reverse direction with the logic flow, with mine creating a waterfall of ifs, and hers starting with the reverse condition and using else ifs. I think I preferred her approach because it allowed her to place her return conditions closer to the logic that preempted them.

  2. (Link) Two things I'd prefer to use that this solution implements: Setting returns to the actual return of the logic, rather than explicitly returning true or false. I also prefer this use of || to get at the edge cases. It's cleaner and shorter.

  3. (Link) I thought this solution was trying to be too clever by checking odd numbered years and it obfuscated my understanding at first. Additionally, I think in the attempt to be clever it overlooked the group of years where it is an even year and should be a leap year, but is not divisible by 400.

  4. (Link) I liked this version for showing all the logic in basically one line, though it also made a little harder to parse it initially (but I knew whatever it was, it would all result in true)

  5. (Link) This was a little messy, using more parenthesises than necessary to be cautious. Other implementations did this logic better in fewer lines. I also don't like reversing an equality with !, preferring to use != in the first place.

Hamming

My solution

  1. (Link) It was good to be reminded that forEach is a potential vector for solving this problem, but I think for loop is quicker and easier than splitting the strings into arrays (though this code was a good reminder that JS isn't like Ruby in making strings automatically array-like). I also thought that raising the argument should be done quicker, before spending the time splitting the strings.

  2. (Link) This solution was very similar to the last in its use of the forEach, but looks a lot cleaner, potentially because of increasing the variable inline with the if condition on line 13 instead of in the block, which I didn't know you could do. It also reminded me that you can increase a variable with the ++ or -- notation, instead of +=. I would potentially consider using forEach instead of a for loop after seeing this, but because I'm a newbie, I didn't realize you could use it without including lo-dash. still confused about stuff

  3. (Link) I looked at this one for its use of the ternary operator, which I often forget is "a thing". It cleans up code a lot, though also requires a little more thought in parsing the line. Otherwise, the same use of forEach.

  4. (Link) This solution still uses the for loop like I did, but increments the counter with the ternary operator. It feels a little redundant to increment a counter by zero, but allows the developer to not have to use the if block. Additionally, the charAt is a little redundant, but I didn't realize it existed so looked it up.

  5. (Link) Another for loop solution, this one by someone who condenses blocks into one line. I was used to that with Ruby, but I still have trouble with the syntax in JS and like seeing it uncompacted. He also uses his own variables in place of "i" for the index, and I think it's enough of a convention that it's jarring and too many characters to replace the "i" in multiple places.

RNA Transcription

My solution

  1. (Link) I wanted to try lo-dash for this, but the methods I tried weren't working quite right, so I decided there was merit to solving it in pure JS as well. This one implemented map (which is a lo-dash method I looked at). This solution splits the string, then uses the map to provide a mapping for each string and rejoins them.
  2. (Link) I also tried to use a replace function, but stalled because replace isn't a mutator, so I couldn't get back the changed string appropriately. This solution does two things I wasn't doing: declaring the replacement value as a function, and explicitly returning it, which might have helped me. My for loop worked fine, but it seems more appropriate to use methods that are meant for strings and not just build one from scratch.
  3. (Link) Something like this feels more Ruby-like, to use an "enumerable" to iterate over a collection. I am still figuring out what requires lo-dash, and what is given with JS, and map is apparently something bestowed by native JS.
  4. (Link) This solution similarly used a for loop with an empty string, but repeated a lot of code with his switch. I do prefer the use of the switch in this kind of case to an if loop, but it still could be drier.
  5. (Link) I was curious about this solution, because he used 5 iterations. Though some turned out to be repeats or potentially nonfunctioning solutions, I did appreciate that he started with split/map and went to replace.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment