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’.

GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^  ^ ^    ^^

The Hamming distance between these two DNA strands is 7.

screen-shot-2016-12-14-at-3-04-37-pm

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

screen-shot-2016-12-14-at-3-25-19-pm

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.");

 

 

 

Advertisements
Exercism JS: Hamming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s