Avatar of brunnock

brunnock's solution

to Protein Translation in the JavaScript Track

Published at Nov 20 2018 · 0 comments
Test suite


This exercise has changed since this solution was written.

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


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



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.


Tyler Long

Submitting Incomplete Solutions

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


import translate from './protein-translation';

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

  xtest('Methionine codon translates into protein', () => {

  xtest('Phenylalanine codons translate into protein', () => {
    expect(translate('UUUUUC')).toEqual(['Phenylalanine', 'Phenylalanine']);

  xtest('Leucine codons translate into protein', () => {
    expect(translate('UUAUUG')).toEqual(['Leucine', 'Leucine']);

  xtest('Serine codons translate into protein', () => {
    expect(translate('UCUUCCUCAUCG')).toEqual(['Serine', 'Serine', 'Serine', 'Serine']);

  xtest('Tyrosine codons translate into protein', () => {
    expect(translate('UAUUAC')).toEqual(['Tyrosine', 'Tyrosine']);

  xtest('Cysteine codons translate into protein', () => {
    expect(translate('UGUUGC')).toEqual(['Cysteine', 'Cysteine']);

  xtest('Tryptophan codon translates into protein', () => {

  xtest('Sequence starts with stop codon 1', () => {

  xtest('Sequence starts with stop codon 2', () => {

  xtest('Sequence starts with stop codon 3', () => {

  xtest('Small RNA strand', () => {
    expect(translate('AUGUUUUCU')).toEqual(['Methionine', 'Phenylalanine', 'Serine']);

  xtest('Stop codon ends translation', () => {
    expect(translate('AUGUUUUCUUAAAUG')).toEqual(['Methionine', 'Phenylalanine', 'Serine']);

  xtest('Invalid codon throws error', () => {
    expect(() => translate('LOL')).toThrow(new Error('Invalid codon'));
const proteins = {
  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'	
function translate(string) {
  let polypeptide=[];
  if (typeof string === 'string') {
    for (let codon of string.match(/.../g)) {
      let protein = proteins[codon];
      if (protein===undefined) throw('Invalid codon');
      if (protein === 'STOP') return polypeptide;
  return polypeptide;
module.exports = translate;

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?