I’ve challenged myself to 30 days of codewars, one a day, time myself, and learn how I could have done it better. And I finally have made it to day 30!! Wow, what a journey!
Today I did: https://www.codewars.com/kata/single-character-palindromes
Time: 8 minutes
What I’ve learnt:
I totally thought this would be impossible! But then I thought about it for a minute, and I was like, hey, that should be easy.
We had three things to check, return “OK” if it is currently a palindrome. That is easy 🙂 string === string.split("").reverse("").join("")
Onto the next challenge. Check if we remove just one letter, will it become a palindrome? If yes, return “return one”.
In a for loop I created a new variable holding the original strings value split into an array (so we don’t obstructively mess up our original array as we iterate), then spliced off the current index (i
), and only one element, newS.splice(i, 1)
. Then checked if it was a palindrome the same way I did above (shoulda made a method for this if I used it twice!) and if it did I returned remove one
so that it would break out of the loop since we have the answer.
And the last line says return "not possible"
since it has not passed those first two tests.
Score!
What I learned from other people’s code:
I see someone using the do...while
. I don’t particularly love their code, but I do like that syntax. It is very readable.
A lot of people made an external function to check if palindrome. That makes sense. Although quite a few actually ran loops to check if it was a palindrome.
Ah, I think if I would have used substring I would not have had to assign to my variable and then remove the letter from it.
I can’t believe I made it this far. It was a great learning journey. I now feel like I can do anything in the world to strings, lol. Have learned new ways to think. Problem solving is coming easier. And I see in how many ways I can grow! Looking forward to an exciting journey as I advance in my programming path. The journey never ends, and the learning never stops!