function checkPalindrome(inputString) {
string = inputString.toLowerCase().replace(/[^a-z0–9]+/g,””);
return string === string.split(“”).reverse().join(“”);

I saw this the other day and wondered what the hell is that and what does it do? It is the solution to a coding exercise asking the programmer to determine whether a string is a palindrome. You could’ve guessed that based on the conveniently named function, checkPalindrome, with the parameter of inputString. People that are new to the programming world, like myself, may also eloquently ask “wtf does the rest do…” Let’s get into that.

string = inputString.toLowerCase().replace(/[^a-z0–9]+/g,””);

We are altering the inputString that was passed into the function and assigning it a new value of string. As you might of guessed, the inputString.toLowerCase() part sets the inputString to lower case, but what is that .replace business?

It negates every thing other than alphanumeric characters. Whatever is inside the square brackets is a character set. A starting cap(^) INSIDE the character set says its a negation. So [^a-zA-Z0-9]* says replace zero or more characters which are other than a-z or 0-9.

The return statement simply asks that if our declared string value is equal to itself after it has been dissected into smaller strings (each containing one letter IE a string of ‘race car’ would become an array containing ‘r’, ‘a’, ‘c’, ‘e’, ‘c’, ‘a’, ‘r’.), then reversing the order that the characters are in within the array, and finally rejoining them into a long string.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.