- 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
Finding the 1-based index of a character in alphabets using JavaScript
Problem
We are required to write a JavaScript function that takes in a lowercase English alphabet character. Our function should return the character’s 1-based index in the alphabets.
Example
Following is the code −
const char = 'j'; const findCharIndex = (char = '') => { const legend = ' abcdefghijklmnopqrstuvwxyz'; if(!char || !legend.includes(char) || char.length !== 1){ return -1; }; return legend.indexOf(char); }; console.log(findCharIndex(char));
Output
10
Advertisements