Reversing consonants only from a string in JavaScript

Problem

We are required to write a JavaScript function that takes in a string of lowercase english alphabets as the only argument.

The function should construct a new string in which the order of consonants is reversed and the vowels hold their relative positions.

For example, if the input to the function is −

const str = 'somestring';

Then the output should be −

const output = 'gomenrtiss';

Example

The code for this will be −

const str = 'somestring';
const reverseConsonants = (str = '') => {
   const arr = str.split("");
   let i = 0, j = arr.length - 1;
   const consonants = 'bcdfghjklnpqrstvwxyz';
   while(i 

Output

And the output in the console will be −

gomenrtiss
Updated on: 2021-03-19T05:48:09+05:30

423 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements