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

dipasreepoddar's solution

to Protein Translation in the JavaScript Track

Published at May 06 2021 · 0 comments
Test suite

Translate RNA sequences into proteins.

RNA can be broken into three nucleotide sequences called codons, and then translated to a polypeptide like so:

RNA: "AUGUUUUCU" => translates to

Codons: "AUG", "UUU", "UCU" => which become a polypeptide with the following sequence =>

Protein: "Methionine", "Phenylalanine", "Serine"

There are 64 codons which in turn correspond to 20 amino acids; however, all of the codon sequences and resulting amino acids are not important in this exercise. If it works for one codon, the program should work for all of them. However, feel free to expand the list in the test suite to include them all.

There are also three terminating codons (also known as 'STOP' codons); if any of these codons are encountered (by the ribosome), all translation ends and the protein is terminated.

All subsequent codons after are ignored, like this:


Codons: "AUG", "UUU", "UCU", "UAA", "AUG" =>

Protein: "Methionine", "Phenylalanine", "Serine"

Note the stop codon "UAA" terminates the translation and the final methionine is not translated into the protein sequence.

Below are the codons and resulting Amino Acids needed for the exercise.

Codon Protein
AUG Methionine
UUU, UUC Phenylalanine
UUA, UUG Leucine
UAU, UAC Tyrosine
UGU, UGC Cysteine
UGG Tryptophan

Learn more about protein translation on Wikipedia

If an invalid character or codon is encountered during translation, it should throw an error with the message Invalid codon.

// => Error: Invalid codon


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



Please cd into exercise directory before running all below commands.

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.

Submitting Solutions

Once you have a solution ready, you can submit it using:

exercism submit protein-translation.js

Submitting Incomplete Solutions

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

Exercise Source Credits

Tyler Long


import { translate } from './protein-translation';

describe('ProteinTranslation', () => {
  test('Empty RNA has no proteins', () => {

  describe('Single codons', () => {
    const mapping = [
      ['Methionine', ['AUG']],
      ['Phenylalanine', ['UUU', 'UUC']],
      ['Leucine', ['UUA', 'UUG']],
      ['Serine', ['UCU', 'UCC', 'UCA', 'UCG']],
      ['Tyrosine', ['UAU', 'UAC']],
      ['Cysteine', ['UGU', 'UGC']],
      ['Tryptophan', ['UGG']],

    mapping.forEach(([protein, codons]) => {
      codons.forEach((codon, index) => {
        const seq = index + 1;
        xtest(`${protein} RNA sequence ${seq} translates into ${protein}`, () => {

    const stopCodons = ['UAA', 'UAG', 'UGA'];

    stopCodons.forEach((codon, index) => {
      xtest(`STOP codon RNA sequence ${index + 1}`, () => {

  describe('Multiple codons', () => {
    xtest('Sequence of two protein codons translates into proteins', () => {
      expect(translate('UUUUUU')).toEqual(['Phenylalanine', 'Phenylalanine']);

    xtest('Sequence of two different protein codons translates into proteins', () => {
      expect(translate('UUAUUG')).toEqual(['Leucine', 'Leucine']);

    xtest('Translate RNA strand into correct protein list', () => {

    xtest('Translation stops if STOP codon at beginning of sequence', () => {

    xtest('Translation stops if STOP codon at end of three-codon sequence', () => {
      expect(translate('AUGUUUUAA')).toEqual(['Methionine', 'Phenylalanine']);

    xtest('Translation stops if STOP codon in middle of three-codon sequence', () => {

    xtest('Translation stops if STOP codon in middle of six-codon sequence', () => {

  describe('Unexpected strands', () => {
    xtest("Non-existing codon can't translate", () => {
      expect(() => translate('AAA')).toThrow(new Error('Invalid codon'));

    xtest("Unknown amino acids, not part of a codon, can't translate", () => {
      expect(() => translate('XYZ')).toThrow(new Error('Invalid codon'));

    xtest("Incomplete RNA sequence can't translate", () => {
      expect(() => translate('AUGU')).toThrow(new Error('Invalid codon'));

    xtest('Incomplete RNA sequence can translate if valid until a STOP codon', () => {
// This is only a SKELETON file for the 'Protein Translation' exercise. It's been provided as a
// convenience to get you started writing code faster.

const mappingCodonProtein = {
  'AUG': 'Methionine', 
  'UUU': 'Phenylalanine', 
  'UUC': 'Phenylalanine', 
  'UUA': 'Leucine', 
  'UUG': 'Leucine', 
  'UCU': 'Serine', 
  'UCC': 'Serine', 
  'UCA': 'Serine', 
  'UCG': 'Serine', 
  'UAU': 'Tyrosine', 
  'UAC': 'Tyrosine',
  'UGU': 'Cysteine', 
  'UGC': 'Cysteine',
  'UGG': 'Tryptophan',
  'UAA': 'STOP',
  'UAG': 'STOP', 
  'UGA': 'STOP'

export const translate = (givenSequence) => {
  var proteinSequence = [];
  if (givenSequence) {
    const codonSequence = givenSequence.match(/.{1,3}/g);
    for (let codon of codonSequence) {
      if (!(codon in mappingCodonProtein)) {
         throw new Error('Invalid codon');
      else if (mappingCodonProtein[codon] == 'STOP') {
      else {
  return proteinSequence;

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?