Complementary DNA (7kyu): Codewars (TDD in JavaScript)
Feb 21, 2020 08:00 · 519 words · 3 minute read
Hi! It’s Friday. So let’s chill and do something easy today. If this is your first time here and you want to learn Frontend and JavaScript, start right now by subscribing, and don’t forget to turn on the subtitles. [Music playing] Let’s solve today kata titled “Complementary DNA.” Deoxyribonucleic acid (DNA) is a chemical found in the nucleus of cells and carries the “instructions” for the development and functioning of living organisms. If you want to know more… No, not this time. In DNA strings, symbols “A” and “T” are complements of each other, as “C” and “G”.
00:45 - You have function with one side of the DNA; you need to get the other complementary side. DNA strand is never empty or there is no DNA at all. More similar exercise are… Okay. DNA_strand (“ATTGC”) return “TAACG” Okay, let’s try. I have a function and… Okay, let’s start with creation of dictionary, hat will be used to replace this string. So for “A” it returns “T,” for “T” it returns “A,” for “C” it returns “G,” for “G” it returns “C. “ And now return DNA and replace all the symbols as following. I need to split this string into characters and… And then map over them… Split. The current element should be replaced from the dictionary, so if this symbol equals “A” then it returns “T” and in this new array a new element will replace. But the dictionary is not a function, it’s an object, so I need square brackets. And then join them. Let’s try. Yes, that works. Okay, now instead of dictionary let’s use a replace method and the regular expressions.
02:34 - For that don’t need this dictionary and I need to return DNA and replace for example, “A” globally to “T.” By the way, the input string always contains only upper case symbols and I will look for uppercase symbols and then replace it by lowercase symbols, because I search for symbols using case sensitive approach and these small symbols will not be replaced by further iterations. So the next iteration will replace “T” globally to “A.” Then the same for “C” to “G,” and then “G” to “C.” And then I need to convert that into the upper case. Let’s try this approach. Yes, it works.
03:40 - Now, let’s combine these two approaches using the replace method and the dictionary. Because the replace method can take a callback function to replace elements. That means I need remove this. Now. I need to take each element and replace it with the functions, so the current element should be replaced by this array… I mean these dictionary so… Now take the current element from this dictionary and now let’s try this solution. Yes it works. I like it. [Music playing] My code is available on GitHub.
04:39 - Documentation is available on the Internet. Follow the links if you’re eager to get more. If you like this video give it “thumbs up”, share it with your friends, subscribe to the channel and watch other episodes. Thanks for watching and dive deeper. .