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

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

Solution:  

screen-shot-2016-12-12-at-5-20-46-pm

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