Can't get to use prototype.map() to replace char by an array - Intermediate Algorithm Scripting: DNA Pairing

Can't get to use prototype.map() to replace char by an array - Intermediate Algorithm Scripting: DNA Pairing
0

Hi There,

I’m new in this forum so Hello ! I’m am currently practicing JavaScript with FCC and for the first time I really can’t get around the reason my script doesn’t work.And I need help. It is my first post here and English is not my first language, I hope it will be clear enough :

So I’m stuck in this exercise intermediate-algorithm-scripting/dna-pairing which consists into pushing array containing pairs of string ["C","G"], ["G","C"], ["T","A"], ["A","T"] depending on a single input made of the first letter of the pair (i.e. “gcg” as input gives a new array [["G","C"], ["C","G"], ["G","C"]] ).

I have read the GetAHint section and understood how my code is not the most elegant / efficient. I tried a .map() solution, here it is :

function pairElement(str) {
     function myFunction(item){
      return  item === "C" ? item.replace("C",["C","G"])
            : item === "G" ? item.replace("G",["G","C"])
            : item === "T" ? item.replace("T",["T","A"])
            : item === "A" ? item.replace("A",["A","T"])
		        :""
      };
   return console.log(str.split("").map(myFunction));
   return str.split("").map(myFunction);
}


pairElement("GCG");

it returns me :

[ 'G,C', 'C,G', 'G,C' ]

My question is : Why my .replace("C",["C","G"]) function returns array of strings separated with commas instead of an array of arrays such as I putted in the function itself ?

Even if the answer in my proposition may not be the brightest, I just would like to understand how it (doesnt) work.

THANK YOU !

I think because replace() is a string method and its return value is a string.

“The replace() method returns a new string …”

This exercise can be solved without replace though …

2 Likes

As @michaelsndr points out, replace is s a string method. If you don’t want to return a string, don’t use it. I feel like you may have gotten a bit lost in how .map() works. Its callback function will be called on every item in an array and will replace each item with the returned value.

Also, chaining ternaries 4 deep is … generally a bad idea. You seem to be using them as a switch statement, so why not just use one of those?

1 Like

Thanks for fast answer !

OK now I get it, that explains why the output returns an array made of strings only.
I feel like I’m trying to do something with the wrong tools anyway.

Thank you again

Yes I definitely wanted to make something “switch like” but didn’t have this reflex.

If I follow you on the .map() method, I could just write it like :

   function myFunction(item){
      return  item === "C" ? ["C","G"]
            : item === "G" ? ["G","C"]
            : item === "T" ? ["T","A"]
            : item === "A" ? ["A","T"]
		        :""
      };

I tried to implement a switch method as you made a point :

function myFunction(item){
      switch(item){
        case "C": return ["C","G"];
        break;
        case "G": return ["G","C"];
        break;
        case "A": return ["A","T"];
        break;
        case "T": return ["T","A"];
        break;
      }
      };

IT WORKS WELL !

Thank you for your help !

1 Like

Good job! You can even tidy that up by removing the break statements. When a return is hit, that ends function execution, so the break statements aren’t required in this case.

1 Like