In ruby I could do this:
def DNA_strand(dna)
base_hash = { 'A' => 'T', 'T' => 'A', 'G' => 'C', 'C' => 'G' }
dna.gsub(/[ATGC]/, base_hash)
end
I could also do this which would be exactly equivalent:
def DNA_strand(dna)
Dna.tr(’ACTG’, ’TGAC’)
end
In JS is there any equivalent method to :tr in ruby?
Currently I can only think of solving this problem in JS like this:
function DNAStrand(dna){
function matchBase(match, offset, string) {
const base = { 'A': 'T', 'T': 'A', 'G': 'C', 'C': 'G' };
return `${base[match]}`;
}
return dna.replace(/[ATGC]/g, matchBase);
}
Any comments will be appreciated!