Replace Method not working correctly?

function pairElement(str) {
var array = str.split(“”)

var array2D = array.map((single) =>
single
.replace(/A/,“A,T”)
.replace(/T/,“T,A”)
.replace(/C/,“C,G”)
.replace(/G/,“G,C”)
.split()
);
console.log(str)
console.log(array)
return array2D;
}

console.log(pairElement(“ATCG”));

console shows:
ATCG
[ ‘A’, ‘T’, ‘C’, ‘G’ ]
[ [ ‘A,T,A’ ], [ ‘T,A’ ], [ ‘C,G,C’ ], [ ‘G,C’ ] ]

I would like to have the 2D array show only 2 letters per inner array. My syntax is the same why are some arrays containing 2 while others contain three?

Think about this logically. You are first replacing every “A” with “A,T”. And then you are replacing every “T” with “T,A”. Do you see that you added a “T” in the first replace and now it is going to be replaced by “T,A” in the second replace? So a single “A” is going to end up as:

"A" -> "A,T" // after first replace
"A,T" -> "A,T,A" // after second replace

Shaw! Thank you Bruce this helps a ton. I switched to only match the first letter. Fixed the issue! You ROCCK!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.