diff --git a/javascript/rna-transcription/rna-transcription.js b/javascript/rna-transcription/rna-transcription.js new file mode 100644 index 0000000..e0d7e99 --- /dev/null +++ b/javascript/rna-transcription/rna-transcription.js @@ -0,0 +1,18 @@ +const changer = (char) => { + switch (char) { + case 'G': + return 'C'; + case 'C': + return 'G'; + case 'T': + return 'A'; + case 'A': + return 'U'; + default: + throw new Error('Invalid input DNA.'); + } +}; + +const toRna = dna => dna.split('').map(changer).join(''); + +export { toRna }; diff --git a/javascript/rna-transcription/rna-transcription.spec.js b/javascript/rna-transcription/rna-transcription.spec.js new file mode 100644 index 0000000..2a5b8b9 --- /dev/null +++ b/javascript/rna-transcription/rna-transcription.spec.js @@ -0,0 +1,40 @@ +import { toRna } from './rna-transcription'; + +describe('Transcriptor', () => { + test('empty rna sequence', () => { + expect(toRna('')).toEqual(''); + }); + + test('transcribes cytosine to guanine', () => { + expect(toRna('C')).toEqual('G'); + }); + + test('transcribes guanine to cytosine', () => { + expect(toRna('G')).toEqual('C'); + }); + + test('transcribes adenine to uracil', () => { + expect(toRna('A')).toEqual('U'); + }); + + test('transcribes thymine to adenine', () => { + expect(toRna('T')).toEqual('A'); + }); + + test('transcribes all dna nucleotides to their rna complements', () => { + expect(toRna('ACGTGGTCTTAA')) + .toEqual('UGCACCAGAAUU'); + }); + + test('correctly handles invalid input', () => { + expect(() => toRna('U')).toThrow(new Error('Invalid input DNA.')); + }); + + test('correctly handles completely invalid input', () => { + expect(() => toRna('XXX')).toThrow(new Error('Invalid input DNA.')); + }); + + test('correctly handles partially invalid input', () => { + expect(() => toRna('ACGTXXXCTTAA')).toThrow(new Error('Invalid input DNA.')); + }); +});