Reversing the alphabet from back to front and front to back in JavaScript


We are required to write a JavaScript function that takes in a single alphabet as the only input. The function should compute the position of the that alphabet from starting and return an alphabet that’s at the same position but from the back.

Example

The code for this will be −

const alpha = 'g';
const findCounterPart = (alpha = '') => {
   let alphabet = 'abcdefghijklmnopqrstuvwxyz';
   let firstpart = alphabet.substring(0,13).split('');
   let secondpart = alphabet.substring(13).split('').reverse();
   let solution = '';
   if (firstpart.indexOf(alpha) !== −1) {
      solution = secondpart[firstpart.indexOf(alpha)];
   } else {
      solution = firstpart[secondpart.indexOf(alpha)];
   };
   return solution;
};
console.log(findCounterPart(alpha));

Output

And the output in the console will be −

t

Updated on: 23-Nov-2020

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements