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 !