-
-
Notifications
You must be signed in to change notification settings - Fork 20
Open
Description
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
Stringiteration (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
Objectto keep track of the mapping instead of conditionals. - Use iteration via
String#splitorString#replaceinstead of usingfor/forEachwithArray#push - Discourage
Array#reducefor this particular solution, because it creates a lot of intermediary strings (more than thesplitapproach), except if the rest of the solution is correct (then you can mention it but approve). Usingreducerequires more interpretation by the reader to follow, change and maintain. - Discourage
String#substringwith foreach iteration, because character iteration viasplit('')is more idiomatic and maintainable thansubstringwith 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
batchrunner to generate output for all fixtures. - go from there by adding paths to disapprove.
Metadata
Metadata
Assignees
Labels
No labels