Complementary DNA
题目:
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 http://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).
翻译(来自有道翻译):
脱氧核糖核酸(DNA)是一种在细胞核中发现的化学物质,携带着生物体发育和功能的“指令”。
如果你想知道更多http://en.wikipedia.org/wiki/DNA
在DNA串中,符号“A”和“T”是互补的,如“C”和“G”。DNA的一边有功能(弦,除了Haskell);你需要得到另一个互补的边。DNA链从来不是空的,或者根本就没有DNA(再说一遍,除了Haskell)。
DNAStrand ("ATTGC") // return "TAACG"
DNAStrand ("GTAT") // return "CATA"
个人解题代码:
//思路:定义一个空字符串str,for循环遍历dna字符串
//使用charAt()方法与"A""T""C""G"做判断,并将结果加在str中,最后输出str
function DNAStrand(dna){
var str = "";
for(var i = 0 ; i < dna.length ; i++){
if(dna.charAt(i) == "A"){
str += "T"
} if(dna.charAt(i) == "T"){
str += "A"
} if(dna.charAt(i) == "C"){
str += "G"
} if(dna.charAt(i) == "G"){
str += "C"
}
}
return str
}
高亮答案:
function DNAStrand(dna) {
return dna.replace(/./g, function(c) {
return DNAStrand.pairs[c]
})
}
DNAStrand.pairs = {
A: 'T',
T: 'A',
C: 'G',
G: 'C',
}