javascript-analyzer
javascript-analyzer copied to clipboard
Add analyzer for rna-transcription
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
-
String
iteration (String#replace
,String#split
) -
Object
/Map
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
orString#replace
instead of usingfor
/forEach
withArray#push
- Discourage
Array#reduce
for this particular solution, because it creates a lot of intermediary strings (more than thesplit
approach), except if the rest of the solution is correct (then you can mention it but approve). Usingreduce
requires more interpretation by the reader to follow, change and maintain. - Discourage
String#substring
with foreach iteration, because character iteration viasplit('')
is more idiomatic and maintainable thansubstring
with 1. Usingsplit('')
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.