Exercism JS: Hamming

Write a program that can calculate the Hamming difference between two DNA strands.

By counting the number of differences between two homologous DNA strands taken from different genomes with a common ancestor, we get a measure of the minimum number of point mutations that could have occurred on the evolutionary path between the two strands.

This is called the ‘Hamming distance’.

^ ^ ^  ^ ^    ^^

The Hamming distance between these two DNA strands is 7.


Another solution that I really like, especially for the error part:


Since I solved this problem in Ruby before, I mostly just spent this time translating it to Javascript.

In brief, you can define a function with the following steps:

  1. Create an anonymous function: var Module = function() {};
  2. Apply that function to Module.prototype.compute = function(arg1, arg2)
  3. Export that anonymous function/module with module.exports
  4. Use var  Module = require(‘./filename.js’); in your file to include that modules.

Here’s a quick & easy to understand guide to Module Patterns.

Also learned how to “throw” an error in Javascript:

First you include this function:

function UserException(message) {
 this.message = message;
 this.name = "UserException";

then you customize the error message in your function:

throw new UserException("DNA strands must be of equal length.");




Exercism JS: Hamming

Exercism JS: Leap Year

Write a program that will take a year and report if it is a leap year.

The tricky thing here is that a leap year in the Gregorian calendar occurs:

  • Every year that is evenly divisible by 4
  • Except every year evenly divisible by 100
  • Unless the year is also evenly divisible by 400



Test cases.

Additional Solutions.

This problem was not challenging at all.  However, I did struggle with the JS fundamentals since I’m not at all familiar with JS yet.  This was good exposure on what to focus one:  module patterns and anonymous closures.

I found some resources here and here that may be helpful to read over.

Exercism JS: Leap Year