Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Nucleotide Count in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given a single stranded DNA string, compute how many times each nucleotide occurs in the string.

The genetic language of every living thing on the planet is DNA. DNA is a large molecule that is built from an extremely long sequence of individual elements called nucleotides. 4 types exist in DNA and these differ only slightly and can be represented as the following symbols: 'A' for adenine, 'C' for cytosine, 'G' for guanine, and 'T' thymine.

Here is an analogy:

  • twigs are to birds nests as
  • nucleotides are to DNA as
  • legos are to lego houses as
  • words are to sentences as...


Go through the setup instructions for JavaScript to install the necessary dependencies:


Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine nucleotide-count.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.


The Calculating DNA Nucleotides_problem at Rosalind http://rosalind.info/problems/dna/

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


var dna = require('./nucleotide-count');

describe('DNA', function () {
  it('Empty DNA strand has no adenosine', function () {

  xit('Repetitive cytidine gets counted', function () {

  xit('Counts only thymidine', function () {

  xit('Counts a nucleotide only once', function () {
    var acid = dna('CGATTGGG');

  xit('Empty DNA strand has no nucleotides', function () {
    var expected = {A: 0, T: 0, C: 0, G: 0};

  xit('Repetitive sequence has only guanosine', function () {
    var expected = {A: 0, T: 0, C: 0, G: 8};

  xit('Counts all nucleotides', function () {
    var expected = {A: 20, T: 21, C: 12, G: 17};

  xit('Validates DNA', function () {
    expect(dna.bind(null, 'JOHNNYAPPLESEED')).toThrow();
function dna(strand) {
  'use strict';

  strand = strand || ""
  var valid_strand = /[^ATGC]/;
  var nucleotides = strand.split("");
  var counts = {
    A: 0,
    T: 0,
    G: 0,
    C: 0

  if (valid_strand.test(strand)) {
    throw "Invalid strand"

  nucleotides.forEach(function(nucleotide) {
    counts[nucleotide] += 1

  function count(nucleotide) {
    return counts[nucleotide] || 0

  function histogram() {
    return counts

  return {
    count: count,
    histogram: histogram


module.exports = dna;

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?