javascript-analyzer icon indicating copy to clipboard operation
javascript-analyzer copied to clipboard

Add analyzer for rna-transcription

Open SleeplessByte opened this issue 5 years ago • 0 comments

This issue is for discussion and assignment for the rna-transcription core exercise in the javascript track.

🔗 implementation | mentor-notes | problem-specification


This exercise focuses on

Optimal solution

const TRANSCRIPTION = {
  C: 'G',
  G: 'C',
  A: 'U',
  T: 'A',
};

export function toRna(sequence) {
  return sequence
    .split('')
    .map(nucleotide => TRANSCRIPTION[nucleotide])
    .join('')
}

Variations include Set or Map with #get, which are valid!

Variations (approvable without comment)

String destructuring can also be used:

return [...sequence].map(/**/).join('')

String#replace can also be used:

return sequence.replace(/./g, (nucleotide) => /* */)

Variations include replacing only the "known" nucleotides:

sequence.replace(/[CGAT]g/, (nucleotide) => /* */)

Helper function variation

Instead of an anonymous function, a helper function may be used:

function transcribe(nucleotide) {
  return TRANSCRIPTION[nucleotide]
}

This also allows for a version without a mapping object:

function transcribe(nucleotide) {
  switch(nucleotide) {
    case 'C': { return 'G' }
    case 'G': { return 'C' }
    case 'A': { return 'U' }
    case 'T': { return 'A' }
  }
}

SHOULD comment and disapprove

Cases we SHOULD comment on with this analyzer:

  • Use an Object to keep track of the mapping instead of conditionals.
  • Use iteration via String#split or String#replace instead of using for/forEach with Array#push
  • Discourage Array#reduce for this particular solution, because it creates a lot of intermediary strings (more than the split approach), except if the rest of the solution is correct (then you can mention it but approve). Using reduce requires more interpretation by the reader to follow, change and maintain.
  • Discourage String#substring with foreach iteration, because character iteration via split('') is more idiomatic and maintainable than substring with 1. Using split('') requires less interpretation by the reader to follow, change and maintain.

Approach

The suggested approach is to:

  • detect which type of solution it is (switch, map, or for/forEach)
  • make sure the optimal solution is correctly handled. You can use the batch runner to generate output for all fixtures.
  • go from there by adding paths to disapprove.

SleeplessByte avatar Sep 26 '19 11:09 SleeplessByte