- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements