
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 < j){ while(i < j && consonants.indexOf(arr[i]) < 0) { i++; } while(i< j && consonants.indexOf(arr[j]) < 0) { j--; } let tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } let result = ""; for(let i = 0; i < arr.length; i++) { result += arr[i]; } return result; }; console.log(reverseConsonants(str));
Output
And the output in the console will be −
gomenrtiss
- Related Articles
- Reversing vowels in a string JavaScript
- Reversing words in a string in JavaScript
- Reversing alphabets in a string using JavaScript
- Reversing words within a string JavaScript
- Reversing words present in a string in JavaScript
- Reversing a string using for loop in JavaScript
- How to delete consonants from a string in Python?
- Reversing the even length words of a string in JavaScript
- Reversing the order of words of a string in JavaScript
- Reversing a string while maintaining the position of spaces in JavaScript
- How to remove consonants from a string using regular expressions in Java?
- Keeping only alphanumerals in a JavaScript string in JavaScript
- Reversing strings with a twist in JavaScript
- Keeping only redundant words in a string in JavaScript
- Function that only replaces character from string after specified appearances in JavaScript

Advertisements