Solution: DNA Pairing - freeCodeCamp (Intermediate Algorithm Scripting)

 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.


SOLUTION:


function pairElement(str) {
  let arr = str.split(""); 
  let arr1 = arr.map(item => {
    switch(item) {
      case 'G':
      return ['G', 'C'];
      case 'C':
      return ['C', 'G'];
      case 'A':
      return ['A', 'T'];
      case 'T':
      return ['T', 'A'];
    }
  })  return arr1;
}

pairElement("GCG");


DNA Pairing - freeCodeCamp (Intermediate Algorithm Scripting)


Click here to go to the original link of the question.

Post a Comment

Previous Post Next Post