Reversing all alphabetic characters in JavaScript

JavascriptWeb DevelopmentFront End Technology

Problem

We are required to write a JavaScript function that takes in a string str. The job of our function is to reverse it, omitting all non-alphabetic characters.

Example

Following is the code −

 Live Demo

const str = 'exa13mple';
function reverseLetter(str) {
   const res = str.split('')
   .reverse()
   .filter(val => /[a-zA-Z]/.test(val))
   .join('');
   return res;
};
console.log(reverseLetter(str));

Output

elpmaxe
raja
Published on 17-Apr-2021 12:06:23
Advertisements