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

rootulp's solution

to RNA Transcription in the TypeScript 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 DNA strand, return its RNA complement (per RNA transcription).

Both DNA and RNA strands are a sequence of nucleotides.

The four nucleotides found in DNA are adenine (A), cytosine (C), guanine (G) and thymine (T).

The four nucleotides found in RNA are adenine (A), cytosine (C), guanine (G) and uracil (U).

Given a DNA strand, its transcribed RNA strand is formed by replacing each nucleotide with its complement:

  • G -> C
  • C -> G
  • T -> A
  • A -> U


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



Install assignment dependencies:

$ yarn install

Making the test suite pass

Execute the tests with:

$ yarn test


Hyperphysics http://hyperphysics.phy-astr.gsu.edu/hbase/Organic/transcription.html

Submitting Incomplete Solutions

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


import Transcriptor from './rna-transcription'

describe('Transcriptor', () => {
  const transcriptor = new Transcriptor()

  it('transcribes cytosine to guanine', () => {

  xit('transcribes guanine to cytosine', () => {

  xit('transcribes adenine to uracil', () => {

  xit('transcribes thymine to adenine', () => {

  xit('transcribes all dna nucleotides to their rna complements', () => {

  xit('correctly handles invalid input', () => {
    expect(() => transcriptor.toRna('U')).toThrowError(
      'Invalid input DNA.'

  xit('correctly handles completely invalid input', () => {
    expect(() => transcriptor.toRna('XXX')).toThrowError(
      'Invalid input DNA.'

  xit('correctly handles partially invalid input', () => {
    expect(() => transcriptor.toRna('ACGTXXXCTTAA')).toThrowError(
      'Invalid input DNA.'

class Transcriptor {
  static readonly NUCLEOTIDE_COMPLEMENTS = new Map<string, string>([
    ['G', 'C'],
    ['C', 'G'],
    ['T', 'A'],
    ['A', 'U']
  public toRna(strand: string) {
    if (this.invalidInputData(strand)) {
      throw 'Invalid input DNA.'
    return this.transcribeStrand(strand)
  private transcribeStrand(strand: string) {
    return strand.split('').map((nucleotide) => this.transcribeNucleotide(nucleotide)).join('')
  private transcribeNucleotide(nucleotide: string) {
    return Transcriptor.NUCLEOTIDE_COMPLEMENTS.get(nucleotide)
  private invalidInputData(strand: string): boolean {
    for (const nucleotide of strand) {
      if (!Transcriptor.NUCLEOTIDE_COMPLEMENTS.has(nucleotide)) {
        return true
    return false


export default Transcriptor

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?