🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of slaymance

slaymance's solution

to Protein Translation in the JavaScript Track

Published at Jun 05 2021 · 4 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', () => {
 * Check out all my solutions to the Exercism JavaScript track:
 * github.com/slaymance/exercism/tree/main/javascript

const STOP = 'STOP';

const invert = obj => Object.fromEntries(
  Object.entries(obj).flatMap(([protein, codons]) => codons.map(codon => [codon, protein]))

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

export const translate = (rna = '') => {
  const protein = TRANSLATIONS[rna.slice(0, 3)];
  if (!protein) throw new Error('Invalid codon');
  return protein === STOP ? [] : [protein, ...translate(rna.slice(3))];

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of aosante

Cool! Could you maybe explain the invert function a bit please?

Avatar of slaymance

Hey @aosante, invert is a helper function which takes an object and "inverts" it by creating a new object where the keys are the values of the original object and the values are the keys. By running the protein mapping above through invert we get:

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

With this object, we can easily do a protein look-up based on each RNA slice we iterate over. However, this object is a bit unwieldy the maintain, and has a lot of repeated values. Thus, I use the invert function and create the object where each protein is mapped to an array of RNA values so I don't repeat myself.

(edited 167 days ago)
Avatar of aosante

Gotcha! Thanks so much for taking the time to explain :)

Avatar of vian

Thanks for your solution. It really does the job well!

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?