This exercise focuses on
String
iteration (String#replace
,String#split
)Object
/Map
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!
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) => /* */)
There are versions that check for valid Input DNA (which has since been removed), meaning they include the following or variations:
function invalidInput() {
throw new Error('Invalid input DNA.')
}
/*...*/
.map(nucleotide => TRANSCRIPTION[nucleotide] || invalidInput())
/*...*/
Since the error handling is now unnecessary, it will be considered defensive programming. The student does not need to validate the input, as the tests no longer ask for it.
- 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.
- Encourage streams because it's harder to use intermediary variables (which can lead to bugs) and forces "one-by-one" approach. It is easier to explain the
Object
solution over multiple conditionals when it's a stream. - If a student uses an
Object
, point them tofalsy
values.