/javascript/nucleotide-count/nucleotide-count.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var dna = function(sequence) {
  sequence = sequence || '';

  // Process the DNA sequence at the start since it won't change.
  var chars = sequence.split('');
  var histogram = {
    A: 0,
    C: 0,
    G: 0,
    T: 0
  };

  chars.forEach(function(c) {
    if (histogram[c] !== undefined) {
      histogram[c]++;
    } else {
      throw 'Invalid character';
    }
  });

  return {
    // Return the full frequency count of nucleotides
    histogram: function() {
      return histogram;
    },

    // Return the number of a particular type of nucleotide
    // found in the DNA sequence
    count: function(nucleotide) {
      return histogram[nucleotide];
    }
  };
};


module.exports = dna;

@lukebrdn thinks this looks great

Comments

This time also passing the last test in the suite... was mystified by the whole bind thing until I remembered it's a thing you do to a function.

helenst commented 30 January 2015 at 14:42 UTC

You're not logged in right now. Please login via GitHub to comment