I’ve already solved this algorithm challenge, so please look away if you don’t want to spoil it for yourself.
I don’t understand the behavior of the code below. It’s what I had right before I fixed it.
function pairElement(str) {
var DNAWrapper = [];
var pairWrapper = [];
var arr = str.split('');
function pushIntoArrs(pairVal) {
pairWrapper.push(arr[i], pairVal);
DNAWrapper.push(pairWrapper);
//pairWrapper = []; uncomment this to pass problem
}
for (var i = 0; i < arr.length; i++) {
if (arr[i] == 'G') {
pushIntoArrs('C');
}
else if (arr[i] == 'C') {
pushIntoArrs('G');
}
else if (arr[i] == 'A') {
pushIntoArrs('T');
}
else {
pushIntoArrs('A');
}
} // close for
return DNAWrapper;
}
pairElement("GCG");
This returns [["G","C","C","G","G","C"],["G","C","C","G","G","C"],["G","C","C","G","G","C"]]
But, I expected it to return [["G","C"],["G","C","C","G"],["G","C","C","G","G","C"]]
due to the for loop. I know this wouldn’t solve the challenge, but I want to understand what’s happening. Thanks for any help!