Palindrome Test

Author: Theodore Odeluga

The source files for this project can be downloaded from here

This is a Node command line project and Node.js can be downloaded from here

Instructions for running a command line program with Node.js can be found here

We all know what a palindrome is – a word which reads the same in reverse (i.e. ‘mum’, ‘dad’, 'pop' or ‘racecar’). There is an equivalent in numbers too – such as 121 for example. Here’s the challenge:

Humans recognise palindromes easily enough but how do we get a computer to do the same?

The answer is to follow an intuitive approach and simply translate the way we understand palindromes – through reading them from left to right and right to left - to source code, so a computer can do what we can do.

Luckily for us, among other tools, JavaScript comes with useful features for string manipulation and we’ll be using some of these features as part of our solution.

To be clear, in this instance, its numbers we are interested in not words.

We’ll effectively be using strings to represent numbers by converting them from one datatype to the other.

Let’s dive in to the program’s only function - palindrometest. To kick things off, numstring converts mynumber to a string object.

//Set original number to string
numtostring = mynumber.toString();

originalelements then breaks up numstring by applying the split method with a comma-based delimiter.

//Split the string
originalelements = numtostring.split("");

The variable j captures the whole length of numstring (in its new form – the originalelements object).

//Capture length of the string
let j = originalelements.length;

The next stage runs a loop containing righttoleft and lefttoright (two arrays traversed by j and k respectively).

/*Read length of string from right to left
and left to right. Push results into the
arrays righttoleft and lefttoright.*/

for(i=0; i < originalelements.length; i++){
--j
righttoleft.push(originalelements[j]);
k++;
lefttoright.push(originalelements[k]);
}

The j iterator runs backwards, through originalelements enumerating in reverse via decrementation from the maximum (the full array size – measured in length) to the lowest value (the first element). K meanwhile does the opposite through incrementing from the lowest value to the maximum value (the last element).

The righttoleft array collects results generated by j using the push method. The lefttoright array does the same with the results generated by k.

lefttoright and righttoleft are then converted by leftstring and rightstring to string objects.

/*Set both arrays to strings ('strings as
as strings' rather than arrays as strings)*/
leftstring = lefttoright.toString();
rightstring = righttoleft.toString();

Nocommasleft then uses the replace method on leftstring to strip out the commas embedded while leftstring was processed purely as an array. Nocommasright does the same with rightstring.

/*Remove commas from both strings (characteristic
left over from when they were treated as arrays).*/
nocommasleft = leftstring.replace(/,/g,'');
nocommasright = rightstring.replace(/,/g,'');

The rationale behind this rather convoluted process of first changing a number to a string (before reversing the change) is to read the initial number provided in the mynumber variable from left to right and then the reverse.

As a palindrome also possesses this characteristic (a kind of left, right symmetry), I considered this the best way to test if the given number was a palindrome.

In order to do this left-right reading, the number first had to be converted to a string, then information gathered by the process stored in two forms – the result of reading from the left and the result of reading from the right, before final conversion back to numeric form.

In the final part of the program, a simple comparison tests to see if both pieces of information (the same number stored as two separate pieces of data) are identical. This should be the case if the number is a palindrome (think about that initial example, 121 – if you read it from the left and also from the right, it’s effectively the same number either way).

Conclusion

As mentioned, the single function in this program was a complicated conveyer belt, in a kind of programmatic conversion to reconversion factory, with two datatypes as the raw materials.

I hope my explanations were intuitive and clear. As per the ethos of this series, I wanted to break down what might at first appear a difficult problem (as many real-world problems are) and again illustrate how taking a simple and straightforward modular approach to a complex task can make the challenge far less onerous. I hope it worked and thanks for reading.