I tinker with javascript from time to time, sometimes solving others’ questions. This post contains interesting findings of my tinkering. It contains 4 examples you can enjoy. Very short and a concise post I think. Here the encounters:

Self-regulating function

One of the toptal interview questions was interesting. Question wanted me to find whether a string is a palindrome or not. For example “madam” is a palindrome, the word is the same when you start to write it backward. It was quite easy:

function isPalindrome(str) {

str = str.replace(/\W/g, '').toLowerCase();

return (str == str.split('').reverse().join(''));

}

There wasn’t any sanitization in my solution, to be honest. Question wanted us to do it under 160 characters. I thought of “how can I check a function is below 160 characters?” and then 5 seconds later I came up with:

`${isPalindrome}`.length < 160

So, if we want to have a self a self-regulating version of this code we can easily wrap the code in an if block. Unfortunately doing that makes it 169 characters.