Это была задачка на codewars, сам я решил ее через if else.
До этого пытался сделать через регулярные выражения, но не получилось. Потом увидел решения других и там было это:
function DNAStrand(dna) {
return dna.replace(/./g, function (c) {
return DNAStrand.pairs[c];
});
}
DNAStrand.pairs = {
A: 'T',
T: 'A',
C: 'G',
G: 'C',
};
console.log(DNAStrand('ATTGC')); // TAACG
Пытался понять что к чему, но так и не понял). Хотелось бы узнать подробное объяснение, СПАСИБО!
Сама задача если кому интересно:
Deoxyribonucleic acid (DNA) is a chemical found in the nucleus of cells and carries the "instructions" for the development and functioning of living organisms.
If you want to know more:
en.wikipedia.org/wiki/DNA
In DNA strings, symbols "A" and "T" are complements of each other, as "C" and "G". You have function with one side of the DNA (string, except for Haskell); you need to get the other complementary side. DNA strand is never empty or there is no DNA at all (again, except for Haskell).
More similar exercise are found here:
rosalind.info/problems/list-view (source)
Example: (input: output)
DNAStrand ("ATTGC") // return "TAACG"
DNAStrand ("GTAT") // return "CATA"