Base pairs are a pair of AT and CG. I'm trying to match the missing element to the provided character and return the results as a 2d array.
When I used the method below, it works fine.
function pair(str) {
str.split("");
//convert the string into an array
var newArray = [];
for (var i = 0; i < str.length; i++){
var subArray = [];
switch (str[i]){
case "G":
subArray.push("G", "C");
break;
case "C":
subArray.push("C", "G");
break;
case "A":
subArray.push("A", "T");
break;
case "T":
subArray.push("T", "A");
break;
}
newArray.push(subArray);
}
return newArray;
}
pair("GCG");
//[["G", "C"], ["C", "G"], ["G", "C"]]
However, when I tried to change the method from push() to splice()as below, it doesn't work.
function pair(str) {
str.split("");
for (var i = 0; i < str.length; i++){
var subArray = [];
switch (str[i]){
case "G":
subArray.push("G", "C");
break;
case "C":
subArray.push("C", "G");
break;
case "A":
subArray.push("A", "T");
break;
case "T":
subArray.push("T", "A");
break;
}
str.splice(i, 1, subArray);
}
return str;
}
pair("GCG");
//ERROR:"str.splice is not a function"
At first I thought the reason why this method failed is that we can't set the third parameter in splice()to be an array. So I tried this:
["G", "C", "G"].splice(0,1,["G","C"]) //["G"]
Looks like it works.
Can anyone show me where am I wrong, please?