Exercism JS: Word Count

Write a program that given a phrase can count the occurrences of each word in that phrase.

For example for the input "olly olly in come free"

olly: 2
in: 1
come: 1
free: 1

Solution in JS: 

screen-shot-2017-01-04-at-9-40-02-pm

I’ve done this problem before in Ruby and it was a breeze.  However, this version in JS was a bit harder because there are more test cases to account for, which was where regex came in.

Advertisements
Exercism JS: Word Count

Exercism JS: Bob

Bob is a lackadaisical teenager. In conversation, his responses are very limited.

  • Bob answers ‘Sure.’ if you ask him a question.
  • He answers ‘Whoa, chill out!’ if you yell at him.
  • He says ‘Fine. Be that way!’ if you address him without actually saying anything.
  • He answers ‘Whatever.’ to anything else.

My Solution in JS:

screen-shot-2016-12-28-at-10-39-51-pm

Things to note:

  • Second part of line 11 is a clever way (that I did not come up with myself) to check if a string is alpha characters or not.  (You can also use regex to check if a string fits a certain criteria for characters.)
  • .trim() in Javascript is the same .strip in Ruby.  (They remove whitespace).
Exercism JS: Bob

Exercism JS: RNA Transcription

Write a program that, given a DNA strand, returns its RNA complement (per RNA transcription).

Both DNA and RNA strands are a sequence of nucleotides.

The four nucleotides found in DNA are adenine (A), cytosine (C), guanine (G) and thymine (T).

The four nucleotides found in RNA are adenine (A), cytosine (C), guanine (G) and uracil (U).

Given a DNA strand, its transcribed RNA strand is formed by replacing each nucleotide with its complement:

  • G -> C
  • C -> G
  • T -> A
  • A -> U

My solution:

screen-shot-2016-12-14-at-11-14-35-pm

Another solution that introduces more advanced concepts like the ‘this’ object constructor and returning a function within a function.

screen-shot-2016-12-14-at-11-03-13-pm

Things to note:

  • ‘this’ allows you to access an object when you’re creating the new object later using the module.  (ie. var dnaTranscriber = new DnaTranscriber();)
  • splitting a string converts it into an array
  • .map allows you to apply the same method to every item in an array.

Javascript is so much harder to grasp than Ruby. 😐

Exercism JS: RNA Transcription

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

 

 

 

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

Exercism JS: Hello World

Write a function that greets the user by name, or by saying “Hello, World!” if no name is given.

My solutions: 

screen-shot-2016-12-09-at-1-30-21-pm

Note to self:  Don’t forget ’em brackets, semi-colons, and ‘===’.

Test Cases:

screen-shot-2016-12-09-at-1-30-56-pm

I was going to skip this problem since I solved it in Ruby already, but good thing I decided not to because JS syntax is a totally different & confusing beast.

More implementations to mull over here.

Also, here’s how to run exercism tests with jasmine-node:

Change ‘xit’ to ‘it’ when you’re ready to run a test case.

Execute the test file with the following code:

cd ~/exercism/javascript/bob
jasmine-node bob_test.spec.js
Exercism JS: Hello World

Exercism Ruby: Gigasecond

Write a program that calculates the moment when someone has lived for 10^9 seconds.

A gigasecond is 10^9 (1,000,000,000) seconds.

My solution (with a lot of digging around Google..)

First, define Gigasecond as a Ruby constant since that shouldn’t ever change.

Then, to figure out the moment someone has lived for that long, take their birthdate and add the defined constant to it.  (No need to do any conversions as the date/time format for the result is no different from format of the given birthdate.)

screen-shot-2016-12-06-at-12-59-28-pm

Here are test cases it should pass.

Notes: Encountered .from for the first time in this problem.  Also learned to define Ruby constants once again.

To submit exercism problems: exercism submit path/to/file.rb

Exercism Ruby: Gigasecond