Skip to content
Home » How to Convert Vowels in a Text to Double Letters using JavaScript

How to Convert Vowels in a Text to Double Letters using JavaScript

This program demonstrates how to take a text input and convert any vowels within it to double letters using JavaScript. The program loops through each character of the input and checks if it is a vowel. If it is a vowel, a specific letter transformation is applied. Finally, the program displays the modified text in uppercase.

Code

const input = 'dude whats up yo';
const vowels = ['a', 'e', 'i', 'u', 'o'];
let resultArray = [];


for (let i=0; i< input.length; i++){
/* prints each item in array
console.log(input[i]);
 prints index of each item in array
console.log(i);
*/

for(let j=0; j<vowels.length; j++){
  if(input[i]===vowels[j]){
    if(input[i]==='e'){
      resultArray.push('ee')
    }
    else if (input[i]==='u'){
      resultArray.push('uu');
    }
    else{
      resultArray.push(input[i]);
    }
  }
}
}
console.log(resultArray.join('').toUpperCase());

Code Explanation

The provided code snippet performs the following steps:

1. Initializes a constant variable named ‘input’ with the value ‘dude whats up yo’.

2. Creates an array named ‘vowels’ containing the vowel letters: [‘a’, ‘e’, ‘i’, ‘u’, ‘o’].

3. Declares an empty array named ‘resultArray’ to store the modified characters.

4. Starts a ‘for’ loop to iterate through each character of the ‘input’ string.

5. Inside the loop, there are comment lines that were meant for debugging but are currently commented out.

6. Another ‘for’ loop is used to check if the current character is a vowel by comparing it with each letter in the ‘vowels’ array.

7. If the current character is a vowel, conditional statements are used to determine the specific letter transformation:

– If the vowel is ‘e’, the string ‘ee’ is pushed to the ‘resultArray’.

– If the vowel is ‘u’, the string ‘uu’ is pushed to the ‘resultArray’.

– Otherwise, the original vowel is pushed as it is to the ‘resultArray’.

8. After both loops execute, the ‘resultArray’ is joined into a single string using the ‘join’ method.

9. The resulting string is then converted to uppercase using the ‘toUpperCase’ method.

10. Finally, the modified text is displayed in the console using the ‘console.log’ function.

Please feel free to try out the provided code snippet and experiment with different input strings to see how the vowel conversions work.

Also checkout the following codes.


“Program to Retrieve Information from an Object in JavaScript”
How to Iterate through an Object in JavaScript – Program to Print Object Key-Value Pairs