DNA Pair BonFire Bug-Please Help

DNA Pair BonFire Bug-Please Help
0

#1

I have seen, and understand other’s code for this bonfire, but I got soooo close on my own, that I want to see if I can get it to work. I’ve looked at my code until I am loopy (pun intended), but I just can’t see the problem.

Here it is in code pen:

http://codepen.io/bleffel/pen/xRwBWY?editors=1111

Thanks for taking a look!


#2

You are iterating through sepLet and you are simultaneously increasing i in for loop and decreasing sepLet.length in your while loop. In result there will be situations where i > sepLet and the first for loop will stop with letters left in sepLet.

I don’t know if my explanation make sense, so here is your code corrected:

Spoiler
function pairElement(str) {
  var firstLet = ["C", "T", "A", "G"];
  var secondLet = ["G", "A", "T", "C"];
  var sepLet = str.split("");
  var dnaOne = "";
  var dnaTwo = "";
  var pair = [];

  for (var i = 0; i < sepLet.length; i++) {
    for (var j = 0; j < firstLet.length; j++) {
      if (sepLet[i] === firstLet[j]) { // replace while with if
        dnaOne = sepLet[i];
        dnaTwo = secondLet[j];
        pair.push([dnaOne, dnaTwo]);
        // sepLet.splice(0, 1); // this line is not needed anymore
      }
    }
  }
  return pair; // add return
}

#3

I used a switch case statement inside a for loop:


Spoiler URL: