gpt4 book ai didi

javascript - DNA 配对,将数组插入数组

转载 作者:行者123 更新时间:2023-11-28 12:25:03 28 4
gpt4 key购买 nike

function pair(str) {

var dna = [];
var dnaarr = [];

for(var i = 0; i < str.length; i++) {

if(str[i].indexOf('G') === 0) {
var a = dna.push('C');
}
if(str[i].indexOf('C') === 0) {
var b = dna.push('G');
}
if(str[i].indexOf('A') === 0) {
var c = dna.push('T');
}
if(str[i].indexOf('T') === 0) {
var d = dna.push('A');
}

}
for(var j = 0; j < str.length; j++) {
var e = dnaarr.push(str[j]);
var f = dnaarr.push(dna[j]);
}

return dnaarr;
}

pair("ATGCG");

当我运行此代码时,它返回

[ 'A', 'T', 'T', 'A', 'G', 'C', 'C', 'G', 'G', 'C' ]

我需要它返回

[['A', 'T'], ['T', 'A'], ['G', 'C'], ['C','G'], ['G', 'C']]

有人可以帮我解决这个代码吗?

最佳答案

这是一个更简单的版本:

function pair(str)
{
// Array to hold the pairs
var dna = [];
// Loop through the string
for (var i = 0; i < str.length; i++) {
// Switch based on the current letter in the string
// Push an array to dna with the current string and it's pair
// in the case of 'G' the array would be ['G','C']
// dna would then be [['G','C']]
switch(str[i])
{
case "G":
dna.push([str[i],"C"]);
break;
case "C":
dna.push([str[i],"G"]);
break;
case "A":
dna.push([str[i],"T"]);
break;
case "T":
dna.push([str[i],"A"]);
break;
};
}
// return the array
return dna;
}

pair("ATGCG")

关于javascript - DNA 配对,将数组插入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30061407/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com