Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Moving all vowels to the end of string using JavaScript
Problem
We are required to write a JavaScript function that takes in a string. Our function should construct a new string in which all the consonants should hold their relative position and all the vowels should be pushed to the end of string.
Example
Following is the code −
const str = 'sample string';
const moveVowels = (str = '') => {
const vowels = 'aeiou';
let front = '';
let rear = '';
for(let i = 0; i < str.length; i++){
const el = str[i];
if(vowels.includes(el)){
rear += el;
}else{
front += el;
};
};
return front + rear;
};
console.log(moveVowels(str));
Output
smpl strngaei
Advertisements
