ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# freddie2025's solution

## to Nucleotide Count in the JavaScript Track

Published at Apr 09 2020 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution 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...

## Setup

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

https://exercism.io/tracks/javascript/installation

## Requirements

Install assignment dependencies:

``````\$ npm install
``````

## Making the test suite pass

Execute the tests with:

``````\$ npm test
``````

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing `xtest` to `test`.

## Source

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.

### nucleotide-count.spec.js

``````import { NucleotideCounts } from './nucleotide-count';

describe('count all nucleotides in a strand', () => {
test('empty strand', () => {
expect(NucleotideCounts.parse('')).toEqual('0 0 0 0');
});

xtest('can count one nucleotide in single-character input', () => {
expect(NucleotideCounts.parse('G')).toEqual('0 0 1 0');
});

xtest('strand with repeated nucleotide', () => {
expect(NucleotideCounts.parse('GGGGGGG')).toEqual('0 0 7 0');
});

xtest('strand with multiple nucleotides', () => {
expect(NucleotideCounts.parse('AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC')).toEqual('20 12 17 21');
});

xtest('strand with invalid nucleotides', () => {
expect(() => NucleotideCounts.parse('AGXXACT')).toThrow(new Error('Invalid nucleotide in strand'));
});
});``````
``````//
// This is only a SKELETON file for the 'Nucleotide-Count' exercise. It's been provided as a
// convenience to get you started writing code faster.
//

export class NucleotideCounts {
static parse(strand) {

let aCount = 0, cCount = 0, gCount = 0, tCount = 0;

for (var i = 0; i < strand.length; i++) {
switch (strand.charAt(i).toUpperCase()) {
case 'A' :
aCount++;
break;
case 'C' :
cCount++;
break;
case 'G' :
gCount++;
break;
case 'T' :
tCount++;
break;
default:
throw new Error('Invalid nucleotide in strand');
}
}
return `\${aCount} \${cCount} \${gCount} \${tCount}`;
}
}``````