dna pairing, pushing array inside an array
dna pairing, pushing array inside an array
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");
When I run this code, it returns
[ 'A', 'T', 'T', 'A', 'G', 'C', 'C', 'G', 'G', 'C' ]
I need it to return
[['A', 'T'], ['T', 'A'], ['G', 'C'], ['C','G'], ['G', 'C']]
Could anyone please help me with this code?
Answer by tkay for dna pairing, pushing array inside an array
It was a problem with your array pushes.
function pair(str) { var dnaarr = []; //var dnatot = []; for(var i = 0; i < str.length; i++) { var dna = []; dna.push(str[i]); //pushing current str[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'); } dnaarr.push(dna); //pushing the array dna to the main array dnaarr } return dnaarr; } console.log(pair("ATGCG"));
Answer by Veo for dna pairing, pushing array inside an array
Here's a simpler version:
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")
Answer by PGGalaviz for dna pairing, pushing array inside an array
Another, probably simpler and cleaner, version of it:
function pair(str) { var arr = str.split(''), newArr = []; arr.forEach(function(e){ if (e == 'A') { newArr.push([e, 'T']); } else if (e == 'T') { newArr.push([e, 'A']); } else if (e == 'G') { newArr.push([e, 'C']); } else { newArr.push([e, 'G']); } }); return newArr; }
Example:
pair('GATC');
will return:
[['G','C'], ['A','T'], ['T', 'A'], ['C', 'G']]
Answer by Mason Shin for dna pairing, pushing array inside an array
function pair(str) { // object of dna paring key, value var pairing = { "A": "T", "T": "A", "C": "G", "G": "C" }, // array to store paired object final = []; // make an array by .split and map that array by using the dna paring object final = str.split('').map(function(val) { var arr = []; arr.push(val, pairing[val]); return arr; }); // return the array of paired objects return final; }
Answer by Yup. for dna pairing, pushing array inside an array
I found this approach to be the most readable. Explanation in comments
function pairElement(str) { // base pairs defined by proj requirements var basePairs = {"A": "T", "T": "A", "C": "G", "G": "C"}; var newGeneCodeArr = []; for (var i = 0; i < str.length; i++) { // build individual sequence pair that fits requirements var newGeneCode = []; newGeneCode.push(str[i]); newGeneCode.push(basePairs[str[i]]); newGeneCodeArr.push(newGeneCode); } return newGeneCodeArr; }
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
ReplyDeletehello guys i have some nice and amazing tips for you all, just follow the link below
http://www.makeoverarena.com/www-wapdam-com-music-video-game-movies/