
- 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 vowels in a string JavaScript
We are required to write a JavaScript function that takes a string as input and reverse only the vowels of a string.
For example −
If the input string is −
const str = 'Hello';
Then the output should be −
const output = 'Holle';
The code for this will be −
const str = 'Hello'; const reverseVowels = (str = '') => { const vowels = new Set(['a','e','i','o','u','A','E','I','O','U']); let left = 0, right = str.length-1; let foundLeft = false, foundRight = false; str = str.split(""); while(left < right){ if(vowels.has(str[left])){ foundLeft = true }; if(vowels.has(str[right])){ foundRight = true }; if(foundLeft && foundRight){ [str[left],str[right]] = [str[right],str[left]]; foundLeft = false; foundRight = false; }; if(!foundLeft) { left++ }; if(!foundRight) { right-- }; }; return str.join(""); }; console.log(reverseVowels(str));
And the output in the console will be −
Holle
- Related Articles
- Reversing words in a string in JavaScript
- Reversing alphabets in a string using JavaScript
- Reversing words within a string JavaScript
- Return Vowels in a string in JavaScript
- Reversing words present in a string in JavaScript
- Reversing a string using for loop in JavaScript
- Reversing consonants only from a string in JavaScript
- Reversing the even length words of a string in JavaScript
- Reversing the order of words of a string in JavaScript
- Counting number of vowels in a string with JavaScript
- Reversing a string while maintaining the position of spaces in JavaScript
- How to get a number of vowels in a string in JavaScript?
- Replacing vowels with their 1-based index in a string in JavaScript
- Reversing strings with a twist in JavaScript
- Remove Vowels from a String in Python

Advertisements