DNA Pairing with flexibility

DNA Pairing with flexibility
0.0 0

#1

Tell us what’s happening:

Anybody get through this with a more flexible solution? mine felt very hard coded, and would obviously break easily.

Your code so far

function pairElement(str) {
  var strArr = str.split("");
  var newArr = [];
  for (i = 0; i < strArr.length; i++){
    switch (strArr[i]){
      case "A":
        newArr.push(["A", "T"]);
        break;
      case "T":
        newArr.push(["T", "A"]);
        break;
      case "C":
        newArr.push(["C", "G"]);
        break;
      case "G":
        newArr.push(["G", "C"]);
        break;
    }
  }
  return newArr;
}

pairElement("GCG");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/65.0.3325.181 Chrome/65.0.3325.181 Safari/537.36.

Link to the challenge:


#2
function pairElement(str) {
  var pairs = [['A', 'T'], ['C', 'G']];
  
  var myPairs = [];
  for (i = 0; i < str.length; i++){
    for(j = 0; j < pairs.length; j++){
      var index = pairs[j].indexOf(str[i]);
      if (index >= 0) {
         myPairs.push([pairs[j][index], pairs[j][3%(index+2)]]);      
      }
    }    
  }
 return myPairs;
}

There are some reasons that this isn’t the best solution, but for whatever reason it’s the one I came up with a couple years ago. While it relies on a hardcoded array, if the the pairs were to change, the code would only need to be updated in one place and it would be easy to change it to take the pairs array as an argument.

Here’s a similar approach using the same basic logic, but much easier to read:

function pairElements(str) {
  const pairs = [['A', 'T'], ['C', 'G']];
  let output = str.split('');
  output = output.map((curr) => {
    for(let pair in pairs) {
      if(pair.contains(curr)) {
        return pair.indexOf(curr) ? pair : pair.reverse();
      }
    }
  });
}

#3

How do I blur? Is there a forum tutorial I missed? lol


#4

image


#5

Why you think it would break easily?


#6

In my view this is the perfect place to use a map alongside a replacement table

It’s straightforward to map a function like (c) => replaceTable[c] over str.split("")


#7

@John-freeCodeCamp here

I used a switch statement. It may not be shorter, but it’s simple and you can easily look back on it later and understand it. It’s only a couple different inputs your dealing with, so all you need is a switch statement.


#8

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.