Intermediate Algorithm Scripting: DNA pairing solution

Here I only share my own code for this challenge. It works.

function pairElement(str) {
  var newStr = str.split('');
  var encapsulateArr =[];
  for(var i = 0; i < newStr.length; i++){
    var insideArr = [];
    insideArr.push(str[i]);
    encapsulateArr.push(insideArr);
  }
  for(var j =0; j < encapsulateArr.length; j++){
    if(encapsulateArr[j][0]==="G"){
      encapsulateArr[j].push("C");
    } else if(encapsulateArr[j][0]==="C"){
      encapsulateArr[j].push("G");
    } else if(encapsulateArr[j][0]==="A"){
      encapsulateArr[j].push("T");
    } else if(encapsulateArr[j][0]==="T"){
      encapsulateArr[j].push("A");
    } else encapsulateArr;
  }

  return encapsulateArr;  
}

pairElement("GCG");

Great work! I wonder if you could think of a way to do it with one loop

1 Like