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