Skip to content

Instantly share code, notes, and snippets.

@duggiemitchell
Last active January 18, 2016 18:27
Show Gist options
  • Select an option

  • Save duggiemitchell/af94c001a30799321f19 to your computer and use it in GitHub Desktop.

Select an option

Save duggiemitchell/af94c001a30799321f19 to your computer and use it in GitHub Desktop.
DNA Pairing
function pair(str) {
// Return each strand as an array of two elements, the original and the pair.
var paired = [];
// Function to check with strand to pair.
var search = function(char) {
switch (char) {
case 'A':
paired.push(['A', 'T']);
break;
case 'T':
paired.push(['T', 'A']);
break;
case 'C':
paired.push(['C', 'G']);
break;
case 'G':
paired.push(['G', 'C']);
break;
}
console.log(char);
};
// Loops through the input and pair.
for (var i = 0; i < str.length; i++) {
search(str[i]);
}
return paired;
}
pair("GCG");

DNA Pairing

The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2d array. Base pairs are a pair of AT and CG. Match the missing element to the provided character. Return the provided character as the first element in each array. For example, for the input GCG, return [["G", "C"], ["C","G"],["G", "C"]] The character and its pair are paired up in an array, and all the arrays are grouped into one encapsulating array.

function pair(str) {
  // Return each strand as an array of two elements, the original and the pair.
  var paired = [];

  // Function to check with strand to pair.
  var search = function(char) {
    switch (char) {
      case 'A':
        paired.push(['A', 'T']);
        break;
      case 'T':
        paired.push(['T', 'A']);
        break;
      case 'C':
        paired.push(['C', 'G']);
        break;
      case 'G':
        paired.push(['G', 'C']);
        break;
    }
    console.log(char);
  };

  // Loops through the input and pair.
   for (var i = 0; i < str.length; i++) {
    search(str[i]);
  }
  return paired;
}
  
pair("GCG");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment