DNA Pair Element Test
2D Array modification from a string arg
by InkAnt908
JavaScript
function pairElement(str){
var arr = [];
var tempArr = [];
//Step 1.
//Take str, push each char to a sub-array,
//Then push that sub-array into the main array & reset
for (var i = 0; i<str.length; i++){
tempArr.push(str[i]);
arr.push((tempArr));
tempArr = [];
}
//Up to this point, we ~should~ have arr = [["G"],["C"],["G"]]
console.log("Sanity check:",arr);
//Step 2.
//Loop through each subarray, add the companion letter to it
arr.forEach(function addPair(arr){
//console.log(arr[j],"is:",arr[j]);
while (arr.length <= 1){
if (arr[0] == "A"){
arr.push("T");
}
else if (arr[0] == "T"){
arr.push("A");
}
else if (arr[0] == "G"){
arr.push("C");
}
else if (arr[0] == "C"){
arr.push("G");
}
else{
console.log("Something is broken :(");
}
console.log("Add pair done, returning:",arr);
return arr;
}
});
console.log("DONE!",arr);
return arr;
}
pairElement("ATCGA");