Skip to content

Instantly share code, notes, and snippets.

@theotherzach
Last active August 29, 2015 13:57
Show Gist options
  • Save theotherzach/9777267 to your computer and use it in GitHub Desktop.
Save theotherzach/9777267 to your computer and use it in GitHub Desktop.
// RE: https://twitter.com/j3/status/448686275751469056
function DNA(sequence){
"use strict";
var displayedMarkers = ['A', 'T', 'C', 'G'];
var acceptedMarkers = displayedMarkers + ['U'];
function checkMarker(marker){
if(acceptedMarkers.indexOf(marker) == -1){
throw(new Error("Invalid Nucleotide"));
};
}
function nucleotideCounts() {
var results = {};
for(var i = 0; i < displayedMarkers.length; i++){
var marker = displayedMarkers[i];
results[marker] = count(marker);
}
return results;
}
function count(marker) {
checkMarker(marker);
return sequence.split(marker).length - 1;
}
// I would probably cheat and change the test to get
// results via method calls rather than accessing properties.
// We can write custom getters to achieve the feel of accessing
// a property, but I would rather steer people away
// from thinking in state and think in commands instead. YMMV
return {
nucleotideCounts: nucleotideCounts,
count: count,
}
}
module.exports = DNA;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment