DEV Community

Cover image for 'A Santa at Nasa?!!!!'
HawkPDX
HawkPDX

Posted on

'A Santa at Nasa?!!!!'

Complex problems have always intrigued me. Anything that can take my overactive mind and allow it to… more so, FORCE IT to, temporarily transition to another dimension.
… a world where i can step away from life’s complexities. I.e. pondering the past, working to keep up with the present AND trippin’ on the future!
That being said, I’m not sure solving this algorithm was the brain vacation I was looking for haha.

While reading up on arithmetic and its place in JavaScript, i came across what’s known as a
‘Palindrome’. Palindromes are a string that when read in reverse, reads the same as it does reading left-to-right.
Initially I thought, how hard can it be?!
My ultimate experience as a ‘Budding Dev Student’ proved the process a bit more involved than original expected but where’s the fun in easy?

ANYWAYS… Less talking, more doing.
Let’s get on with it!

As always, I started with a plan. Nothing crazy, just a few words or partial sentences placed in specific order (probably only understood by me). As a relatively new practice I’ve found with larger/more complicated Deliverables, breaking them down to simpler, easier to attain, mini goals take away some of the anxieties of a problem. I put a checkmark next to each goal as it’s been addressed which helps reinforce (really.. ‘trick’) my mind will each small victory!

OK, for real now…
First, i knew i wanted a descriptive name for my variable, in this attempt, I went with a function. I called it ‘isPalindrome’ in reference to my goal.

Second, I assigned the function a ‘placeholder’ variable. In this case, ’str’ seemed sufficient.

With the easy part out the way, I ran in to my first real challenge. I found that I needed to breakdown the problem even further if I was going to make this work.
Back to the drawing board, so to speak. I referred back to my plan and added a couple sub-steps:

Task number one, convert all characters within the provided string to match ‘case-wise’… I guess I really could have gone either way but I chose the ‘.toLowerCase()’ method.
Sweet! One of two, out the way…. next, one of my challenges was to have the ability to compare strings of any length, to include spaces, sentences, as well as symbols and other non-alphanumeric qualities.

  1. function ‘isPalindrome’ is created
  2. function takes placeholder, ‘str’, as input
  3. ‘alphanumericDelete’ variable is created: This variable will be asked to some things:

    • # destruct the original string.
    • # iterate over each element
    • # convert the input string ‘.toLowerCase’ using the built-in method.
    • # remove anything that is not a number or letter. — this process was the most taxing, it required a bit of googling but I was able to find another built-in regular expression that did exactly what I needed! — this particular ‘Regex’ uses this syntax, in addition to previous methods to basically completely destruct each string, convert to an Array, do the work provided in parameters, ‘join’ all the characters back together and rebuild/output our ‘baby’! Finally, one last step… after completing our complex requests, it takes the values on each side of the === qualifier and check to see whether or not they match. And by ‘match’ i mean the string on the left (provided string) matches the declared value on the opposite side of === which applies reverse() to compare the string in… reverse! Haha, what a concept!

    Here’s what I came up with:

    function isPalindrome(str) { 
    const alphanumericDelete = str.toLowerCase().match(/[a-z0-9]/g);
    return alphanumericDelete.join(“”) === alphanumbericDelete.reverse().join(“”);
    };  
    

    Some test cases:
    console.log(isPalindrome('A man, a plan, a canal: Panama')); // true
    console.log(isPalindrome('race car'));
    console.log(isPalindrome('Was it a car or a cat i saw?'))
    console.log(isPalindrome('A Santa at Nasa?!!!!')

Now it’s your turn!
Copy the included code into your console and try out a word or phrase of your own!
In fact, I challenge you to think one up for yourself… if you’re going to cheat, at least make it something that doesn’t show up in the first few google results :P

Top comments (0)