You will get a DNA strand sequence and you need to get the pair and return it as a 2D array of the base pairs. Keep in mind that the provided strand should be first always.
Another way to interpret the problem: there are four potential characters that exist in DNA: “A”, “T”, “G”, and “C”. “A” and “T” are always paired together, and “G” and “C” are always paired together.
This problem presents you with an input, e.g. “ATCGA”. Each of those five characters are missing their pairs.
E.g. the first character “A” needs to be paired with “T” to give the array element [“A”, “T”].
The second character “T” needs to be paired with “A” to give the array element [“T”, “A”].
The number of elements in the final output equals the number of characters in the input.
This problem does not involve rearranging the input into different combinations or permutations.
A switch would be a natural option here, because each character in the string has 4 possible values.
Hint 3
The result must be an array of arrays.
Solutions
Solution 1 (Click to Show/Hide)
function pairElement(str) {
// Function to match each character with the base pair
const matchWithBasePair = function(char) {
switch (char) {
case "A":
return ["A", "T"];
case "T":
return ["T", "A"];
case "C":
return ["C", "G"];
case "G":
return ["G", "C"];
}
};
// Find pair for every character in the string
const pairs = [];
for (let i = 0; i < str.length; i++) {
pairs.push(matchWithBasePair(str[i]));
}
return pairs;
}
// test here
pairElement("GCG");
Code Explanation
Inside of the matchWithBasePair function, a switch is used to cover all four possible characters. Using if statements is another option.
Create an empty array and use the matchWithBasePair function to push the right values to the array and return them.
function pairElement(str) {
// create object for pair lookup
const pairs = {
A: "T",
T: "A",
C: "G",
G: "C"
};
// map character to array of character and matching pair
return str
.split("")
.map(x => [x, pairs[x]]);
}
// test here
pairElement("GCG");
Code Explanation
First define an object with all pair possibilities, this allows us to easily find by key or value.
Split str into a characters array so we can use each letter to find its pair.
Use the map function to map each character in the array of individual characters to an array with the character and its matching pair, creating a 2D array.
function pairElement(str) {
var newArr = [];
for (var i = 0; i < str.length; i++){
if(str[i] === "C"){
newArr.push((str[i] + "G").split(""));
} else if (str[i] === "G"){
newArr.push((str[i] + "C").split(""));
} else if (str[i] === "T"){
newArr.push((str[i] + "A").split(""));
} else if (str[i] == ="A"){
newArr.push((str[i] + "T").split(""));
}
}
return newArr;
}
I was wondering how to paste my code nicely here in the forums. I will research how to do it but any suggestions/help/hints would be really appreciated.
function pairElement(str) {
var dnArray = []; //creates a new array into which we will push new arrays
for (var i=0;i<str.length;i++){ //iterates through the str
if (str.charAt(i) === "G"){ //if the current str character is a G...
dnArray.push(["G", "C"]); //...push this array into dnArray
} else if (str.charAt(i) === "C"){ //if the current str character is a C...
dnArray.push(["C", "G"]); //push this arra into dnArray
} else if (str.charAt(i) === "A"){ //and so on...
dnArray.push(["A", "T"]);
} else if (str.charAt(i) === "T"){
dnArray.push(["T", "A"]);
}
}
return dnArray; //return the, now populated, dnArray
}
pairElement("ATCGA");
This is my solution with comments. I didn’t find that using if-statements took that much more code than using the switch method.
Probably not the most efficient solution, but for beginners like myself, it seems like possibly the easiest/simplest since I only used very basic string and array methods as well as one for loop and if statements.
function pairElement(str) {
//set up a new array, to store the arrays you will be making for the final product
var newArray = [];
//split the string into an array in order to acces each character of the string easier
var array = str.split('');
//loop through your array
//create if statements for each possible input
//use "if statement breakpoints" to push newly paired arrays into NewArray
for (var i = 0; i < array.length; i++)
if (array[i] == "G"){
newArray.push(["G","C"]);
} else if (array[i] == "C"){
newArray.push(["C","G"]);
} else if (array[i] == "A"){
newArray.push(["A","T"]);
} else if (array[i] == "T"){
newArray.push(["T","A"]);
}
//return new paired multi-dimensional array
return newArray;
}
pairElement("GCG");