freeCodeCamp Challenge Guide: DNA Pairing

You can avoid the β€œelse ifs” by declaring an object in advance:

function pairElement(str) {
  var pairs = {
    G: 'C',
    C: 'G',
    A: 'T',
    T: 'A'
  };
  return str.split('').map(function(element) {
    return [element, pairs[element]];
  });
}

pairElement("GCG");
6 Likes

My solution uses a nested for loop that compares the value of the first index of each subarray in the variable basePairs with the value of each index in the argument str. If true, then it pushes the value to a new array dnaPairs.

function pairElement(str) {
 var basePairs = [["G","C"],["C","G"],["A","T"],["T","A"]];
 var dnaPairs = [];
 for (var i = 0; i < str.length; i++){
   for(var j = 0; j < basePairs.length; j++){
     if (str[i] === basePairs[j][0]){
       dnaPairs.push(basePairs[j]);
    }
   }
  }
  return dnaPairs; 
}

pairElement("GCG");
5 Likes

I used an object to set up the pairs, converted the str into an array and then used map to iterate over it. On each iteration I checked if the current argument is a property of the object and push the key value pair into a new Array.

function pairElement(str) {
    var basePair = {
        "A":"T",
        "T":"A",
        "C":"G",
        "G":"C"
    };
    
    var result = [];
    var strToArr = str.split("").map(function(elem) {
        if(basePair.hasOwnProperty(elem)) {
	       result.push(new Array(elem, basePair[elem]));
        }
    });
  
  return result;
}

pairElement("GCG");
2 Likes

  • ended up with a lot of code to accomplish this
  • i wanted you use a switch but it didn’t work out for me
  • now that i see the basic solution i see that what they did is actually pretty clever
  • the tricky part of mine is when i do my first for loop and make my array multidimensional instead of flat
  • it is interesting that in both cases arr.length = 3
  • ie. [1,2,3].length equals [[1],[2],[3]].length
indent preformatted text by 4 spaces
function pairElement(str) {
  
  var arrayStr = str.split("");
  var newStr = [];
  
  
  for (var i = 0; i < arrayStr.length; i++ ) {

if (arrayStr[i] === "C") {
  newStr[i] = arrayStr[i].split("").concat("G");
}

if (arrayStr[i] === "G") {
  newStr[i] = arrayStr[i].split("").concat("C");
}

if (arrayStr[i] === "A") {
  newStr[i] = arrayStr[i].split("").concat("T");
}

if (arrayStr[i] === "T") {
  newStr[i] = arrayStr[i].split("").concat("A");
}

  }
      
  return newStr;
       
}

pairElement("GCG");

This is my solution. First time using a switch statement in a while.


function pairElement(str) {
  //splits string into array of characters
  str = str.split("");
  
  //iterates through characters to match them up
  for (var i = 0; i < str.length; i++) {
    switch (str[i]) {
      case "G":
        str[i] = str[i] + "C";
        break;
      case "C":
        str[i] = str[i] + "G";
        break;
      case "A":
        str[i] = str[i] + "T";
        break;
      case "T":
        str[i] = str[i] + "A";
    }
    //separates strands into array of arrays
    str[i] = str[i].split("");
  }
  
  return str;
}

pairElement("CTCTA");

function pairElement(str) {
var a = [[β€œA”, β€œT”], [β€œT”, β€œA”], [β€œG”, β€œC”], [β€œC”, β€œG”]];
var arr = str.split("");
var arr1 = [];
for (var i = 0; i<arr.length; i++)
{
for (var j = 0; j<a.length; j++)
{
if (a[j][0].indexOf(arr[i])!=-1)
{
arr1.push(a[j]);
}
}
}
return arr1;
}

pairElement(β€œGCG”);

Hello everyone,
here’s what I have so far, for this challenge:

   function pairElement(str) {
      var dna = [];
      for (var x = 0; x < str.length; x++)
      {  
          dna[x] = (str[x] + {A: 'T', T: 'A', C: 'G', G: 'C'}[str[x]]).split('');
      }
      return dna;
    }

    pairElement("ATCGA");
1 Like

I made the base pairs into a string (β€œATCG”) and then devised a formula to find the index of partners in a string of consecutive pairs. A little overkill, what with only two pairs, but I just love any opportunity to use the modulo operator!

function pairElement(str) {
  var basePairs = "ATCG";
  var strand = [];
  
  for (var i = 0; i < str.length; i++) {
    var pair = [];
    pair.push(str[i]);
  
    var bpIndex = basePairs.indexOf(str[i]);
    var partnerIndex = bpIndex + 1 + (-2 * (bpIndex % 2));
    pair.push(basePairs[partnerIndex]);
    
    strand.push(pair);
  }
  return strand;
}
1 Like

Hello coders!

I don’t know from what part of my mind this came out, but it works:


function pairElement(str) {
      
      var dnaX = "ATCG".split(""),
          dnaY = "TAGC".split(""),
          xyDna = [],
          x = str.split(""),
          y = [];
      
      for(var i = 0; i < x.length; i++){
        for(var j = 0; j < dnaX.length; j++){
          if(x[i] === dnaX[j]){
          y.push(dnaY[j]);
          }
        }
      }

      xyDna.push([x[0],y[0]],[x[1],y[1]],[x[2],y[2]],[x[3],y[3]],[x[4],y[4]]);
      
      return xyDna;
    }

    pairElement("CTCTA");

Thanks for reading!

1 Like

Hi there, just dropping in my solution as it seems to be slightly different from those posted here so far:

function pairElement(str) {
  var pairs = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'};
  var myPairs = str.split('');
  for(var i = 0; i < myPairs.length; i++){
    myPairs[i] = myPairs[i].split();
    myPairs[i].push(pairs[myPairs[i][0]]);
  }
  return myPairs;
}

function pairElement(str) {

var newstr = str.toUpperCase().split(’’);
var arr = [];

for(var i=0; i<newstr.length;i++){
if(newstr[i]===β€œA” && (newstr[i+1]!=β€œT” || newstr[i+1]===β€œT”)){
arr.push([β€˜A’,β€˜T’]);
} else if(newstr[i]===β€œT” && (newstr[i+1]!=β€œA” || newstr[i+1]===β€œA”)){
arr.push([β€˜T’,β€˜A’]);
} else if(newstr[i]===β€œG” && (newstr[i+1]!=β€œC” || newstr[i+1]===β€œC”)){
arr.push([β€˜G’,β€˜C’]);
} else if(newstr[i]===β€œC” && (newstr[i+1]!=β€œG” || newstr[i+1]===β€œG”)){
arr.push([β€˜C’,β€˜G’]);
}
}

return arr;
}

pairElement(β€œATCGA”);

My solution (Just started learning ES6 syntax)

let pairElement = (str) => {
    let DNA = { "A": ["A", "T"], "T": ["T", "A"], "C": ["C", "G"], "G": ["G", "C"] };
    let strArr = str.split("");
    let DNA_Pairs = [];
    for (i = 0; i < strArr.length; i++) {
        let value = strArr[i];
        DNA_Pairs.push(DNA[value]);
    }
    return DNA_Pairs;
};
1 Like

My code here

function pairElement(str) {
var paired = [];
var search = function(arg) {

    switch (arg) {
        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;

    }
};
for (var i = 0; i < str.length; i++) {
    search(str[i]);
}
console.log(paired);

}

I looked at hint but than map function came to my mind

function pairElement(str) {
  return str.split("").map(function(x){
    return x==="G"?["G","C"]:x==="C"?["C","G"]:x==="A"?["A","T"]:x==="T"?["T","A"]:"";
  } );
}

or using arrow function
return str.split("").map(x => x==="G"?["G","C"]:x==="C"?["C","G"]:x==="A"?["A","T"]:x==="T"?["T","A"]:"");

1 Like
/* jshint esversion: 6 */
function pairElement(str) {
  const pairs = {
    A: 'T',
    T: 'A',
    C: 'G',
    G: 'C'
  };
  
  return str.split('').reduce((acc, letter) => {
    acc.push([letter, pairs[letter]]);
    return acc;
  }, []);
}

This is probably an advance solution and it makes use of Array reduce method.

You start the reduce function with an empty array, then at each step, push the new pairs of letters.

1 Like

horrible solution i guess but is the very first solution i was able to get done with no help at all im so happy!!! lol

function pairElement(str) {
return str.split(’’).reduce(function(a,b){
if(b === β€˜G’){
b = β€˜GC’;

} else if(b === 'C'){
  
  b = 'CG';
} else if(b === 'A'){
  
  b = 'AT';
} else if(b === 'T'){
  
  b = 'TA';
  
} 

a.push(b.split(''));
return a;

},[]);
}

pairElement(β€œGCG”);

4 Likes