# tutagomes's solution

## to Nucleotide Count in the TypeScript Track

Published at Nov 11 2019 · 0 comments
Instructions
Test suite
Solution

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 TypeScript to install the necessary dependencies:

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

## Requirements

Install assignment dependencies:

``````\$ yarn install
``````

## Making the test suite pass

Execute the tests with:

``````\$ yarn 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 `xit` to `it`.

## 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.test.ts

``````import NucleotideCount from './nucleotide-count'

describe('count all nucleotides in a strand', () => {
it('empty strand', () => {
const expected = {
A: 0,
C: 0,
G: 0,
T: 0
}
expect(NucleotideCount.nucleotideCounts('')).toEqual(expected)
})

xit('can count one nucleotide in single-character input', () => {
const expected = {
A: 0,
C: 0,
G: 1,
T: 0
}
expect(NucleotideCount.nucleotideCounts('G')).toEqual(expected)
})

xit('strand with repeated nucleotide', () => {
const expected = {
A: 0,
C: 0,
G: 7,
T: 0
}
expect(NucleotideCount.nucleotideCounts('GGGGGGG')).toEqual(expected)
})

xit('strand with multiple nucleotides', () => {
const expected = {
A: 20,
C: 12,
G: 17,
T: 21
}
expect(NucleotideCount.nucleotideCounts('AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC')).toEqual(expected)
})

xit('strand with invalid nucleotides', () => {
const expected = 'Invalid nucleotide in strand'
expect(() => {NucleotideCount.nucleotideCounts('AGXXACT')}).toThrowError(expected)
})
})``````
``````class NucleotideCount {
static nucleotideCounts(frase: string) {
// console.log(regexNucleotideo.test(frase));
for (let i = 0; i < frase.length; i++) {
if (
frase[i] !== "C" &&
frase[i] !== "G" &&
frase[i] !== "T" &&
frase[i] !== "A"
) {
throw new Error("Invalid nucleotide in strand");
}
}
let obj = {
A: frase.split("").reduce((antes, atual) => {
return antes + (atual === "A" ? 1 : 0);
}, 0),
T: frase.split("").reduce((antes, atual) => {
return antes + (atual === "T" ? 1 : 0);
}, 0),
C: frase.split("").reduce((antes, atual) => {
return antes + (atual === "C" ? 1 : 0);
}, 0),
G: frase.split("").reduce((antes, atual) => {
return antes + (atual === "G" ? 1 : 0);
}, 0)
};
return obj;
}
}

export default NucleotideCount;``````