Reverse Number

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

This example reuses code from the Palindrome Test script.

Similar to the other program, this is an exercise in datatype conversion, mainly operating on numbers and strings.

To begin, the mynumber variable is converted to a string.

//Convert mynumber to a string (numtostring)
let numtostring = mynumber.toString();

The next line splits the string with splitit.

//Split numtostring with commas (splitit)
splitit = numtostring.split("");

Treating splitit as an array, j includes all of the object’s elements.

//Initialise j to include the whole of splitit
let j = splitit.length;

The next line opens the function’s loop – which forms the bulk of the program.

//Iterate over splitit with i

for(i=0; i < splitit.length; i++){

/*Simultaneously, iterate backwards through splitit with j
(Starting with the last array element, instead of the first,
moving backward through the array right to left instead of left to right)*/

--j

//Put the 'backward' result into the 'reverse' array
reverse.push(splitit[j]);

//Convert the 'reverse' array to a string
numtostring = reverse.toString(reverse);

//Strip out all commas from the new string
nocommas = numtostring.replace(/,/g,'');

}

//Output the result
console.log(nocommas);

The i iterator traverses the length of splitit and while doing so performs the following actions.

First, j is decremented at each iteration (having initialised with a value equivalent to the length of splitit), then for each iteration, an element of splitit is pushed into another array – "reverse".

The “backward counting” done by j effectively reverses the string contained in numtostring. Numtostring is then reused as its value is reassigned to converting the reverse array back to a string object.

After this, Nocommas uses the replace method to strip out all the commas inserted by splitit (and at all stages of array manipulation). The function of replace is based on a regular expression technique where all comma characters are specifically selected and isolated by the opening and closing forward slashes. In regex, the global operator (signified by the ‘g’) applies a change to the whole string where the comma is replaced by an empty space (indicated by the blank quotes at the end of the method call).

Here's the code of the whole solution.

let i;
let splitit;
let numtostring;
let mynumber = 0;
let reverse = [];
let nocommas;

function reversenumber(mynumber){

//Convert mynumber to a string (numtostring)
let numtostring = mynumber.toString();

//Split numtostring with commas (splitit)
splitit = numtostring.split("");

//Initialise j to include the whole of splitit
let j = splitit.length;

//Iterate over splitit with i
for(i=0; i < splitit.length; i++){

/*Simultaneously, iterate backwards through splitit with j
(Starting with the last array element, instead of the first
(moving backward through the array right to left instead of left to right)*/

--j

//Put the 'backward' result into the 'reverse' array
reverse.push(splitit[j]);

//Convert the 'reverse' array to a string
numtostring = reverse.toString(reverse);

//Strip out all commas from the new string
nocommas = numtostring.replace(/,/g,'');

}

//Output the result
console.log(nocommas);

}

reversenumber(123456789);

Conclusion

The functionality described above was seen earlier in the Palindrome Test program but for the sake of completion and flexibility was hived off here as its own separate solution. This marks the end of the problem-solving series so far (I’ll try to produce more material over time). I hope you’ve enjoyed what you've found here and that its been useful. Thanks for reading and happy coding.