0

Base pairs are a pair of AT and CG. I'm trying to match the missing element to the provided character and return the results as a 2d array.

When I used the method below, it works fine.

 function pair(str) {
     str.split("");
    //convert the string into an array
     var newArray = [];
     for (var i = 0; i < str.length; i++){
       var subArray = [];
       switch (str[i]){
         case "G":
           subArray.push("G", "C");
           break;
         case "C":
          subArray.push("C", "G");
           break;
         case "A":
           subArray.push("A", "T");
           break;
         case "T":
           subArray.push("T", "A");
           break;
       }
       newArray.push(subArray);
     }

      return newArray;
    }

    pair("GCG");
    //[["G", "C"], ["C", "G"], ["G", "C"]]

However, when I tried to change the method from push() to splice()as below, it doesn't work.

function pair(str) {

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

}

pair("GCG");
//ERROR:"str.splice is not a function"

At first I thought the reason why this method failed is that we can't set the third parameter in splice()to be an array. So I tried this:

["G", "C", "G"].splice(0,1,["G","C"])   //["G"]

Looks like it works.

Can anyone show me where am I wrong, please?

2
  • Are you using iframes or tab pages? Commented Jun 18, 2015 at 7:20
  • @MatteoTassinari lol, yep. Commented Jun 18, 2015 at 7:38

3 Answers 3

2
str.splice(i, 1, subArray);

str is string.

.splice() method of Array

Check your logic ( or typo possible there should be subArray )

subArray.splice(i, 1, subArray);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks !!! The split() method returns a new array without change the original string. Thanks for your help!
1

string.split() function does not mutate itself. It returns an array and leave itself as unchanged as string. Looks like you want to split your str into individual characters from this line of code str.split("") and process. So see this code below:

function pair(str) {

    var tokens = str.split("");
    for (var i = 0; i < tokens.length; i++){
        var subArray = [];
        switch (tokens[i]){
         case "G":
              subArray.push("G", "C");
               break;
         case "C":
              subArray.push("C", "G");
               break;
         case "A":
               subArray.push("A", "T");
       break;
         case "T":
               subArray.push("T", "A");
               break;
       }
       tokens.splice(i, 1, subArray);
  }
  return str; // Not sure if you intend to return original str or not
              // So please see my note below:
}

I'm not sure at the bottom part of your code whether you indeed want to return the string.

NOTE: If you want to append those characters in subArray back to str and return, you can do this:

return tokens.concat(subArray).join('');

Comments

0

String.prototype.split doesn't change the string into an array, it returns an array. So you can do this str = str.split('');

Your original function worked because you can access strings with [] as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.