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