- 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 alphabet from ASCII value without using library functions in JavaScript
Problem
We are required to write a JavaScript function that takes in a number. Our function should return the corresponding ASCII alphabet for that number (if there exists an alphabet for that ASCII value), -1 otherwise.
The condition here is that we cannot use any inbuilt function that converts these values.
Example
Following is the code −
const num = 98; const findChar = (num = 1) => { const alpha = 'abcdefghijklmnopqrstuvwxyz'; if(num >= 97 && num <= 122){ return alpha[num - 97]; }; if(num >= 65 && num <= 90){ return alpha.toUpperCase()[num - 65]; }; return -1; }; console.log(findChar(num));
Output
b
Advertisements