Exes and Ohs (7kyu): Codewars (TDD in JavaScript)
Mar 20, 2020 13:44 · 620 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 codewars’ kata titled “Exes and Ohs.” Check to see if a string has the same amount of ‘x’s and ‘o’s. The method must return a boolean and be case insensitive. The string can contain any char.
00:37 - What’re examples? And this one this one is the most interesting. When no xs or os is present should return true, okay, let’s do that. Let’s start with the straightforward for resolution and calculate each Xs and Os and if the sum of plus Xs minus Os is zero, that means that there are the same amount of them. Let’s implement that. Let equal is 0, then string split and for each do the following. Take this character, if… And then let’s use the “joda notation.” X is the current character to lower case, then equal plus one.
01:31 - Otherwise, if Os is the current character to lower case, then equal minus one. And return the comparison. If equal strictly equals to 0 then this function returns true, otherwise it returns false. Let’s try this solution. Oh, a silly typo. Let’s fix this and try again. Good. Now it works. Now let’s optimize this solution as follows. This split can be replaced by a string literal it’s a totally the same and now I can use a short hand here. If this comparison is true, then do the following.
02:25 - Increment equal and do the same for the next comparison like this. Let’s test it. That’s correct. Now I can solve this kata using a reduced method. Let’s do that. I remove everything and do this. String split and reduce accumulator and the current value. If X is the current character then implement our accumulator otherwise decrement it. Here or return accumulator plus one. Else if here and compare with O. Then here a minus one. And I need else comparison for the cases when I should do nothing. And here 0 is an initial value.
03:30 - Now I can compare our result with the expected 0. If it equals then it returns true and that means that there are the same value of X and O. And let’s use an arrow function for our solution. Let’s check. No no, it should be constant not “cosnt”. Let’s try again. Good it works. Now I can modify my comparison and use a short hand here. I’ll use a ternary here. Let’s do that. Remove this one and this one. So if X is the current character, then increment accumulator.
04:28 - However, you know, I can shift this “to lower case” to another place. Here. Now let’s check for Os. Then reduce accumulator, otherwise do nothing. Let’s test. Good that’s correct. However, I can modify my solution using regular expressions. So I need to calculate X characters and O characters in my string and compare them using match operator. Let’s do that. Remove everything. Calculate Xs globally and insensitively. And calculate Os. Now return if there Xs and it’s amount equal to the quantity of Os, then return true, let’s try the solution. And against all the test cases. Yes, that’s correct.
05:40 - [Music playing] My code is available on GitHub. Documentation is available in 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. .